How can i get the parent process only?

Hello,

I'm running one script every night and that's kick off through cron jobs.

Inside the script i'm checking whether my script is already running or not.

I'm using following command to check whether is running or not.

ps -eaf | grep "test_PID.ksh" | grep -v "grep test_PID.ksh" | wc -l

sometime it gives me 2 process or 3 process

Output:
user 15243 15203 0 12:00:01 ? 0:00 /usr/bin/ksh /usr/local/my/bin/test_PID.ksh

user 15203 254 0 12:00:01 ? 0:00 sh -c /usr/local/my/bin/test_PID.ksh

Why am i seeing two process for one job.

How can i get only parent process not child process.

Thank you for your helps in adv.

why not create a scriptname.PID while the script is running. At the end of the scirpt it can remove this. Then the script can check for the existing of the .PID file and exit if it exists.

thought?

Try this :

ps -p $$ -o %P | sed -n 's/^ *\([0-9][0-9]*\) *$/\1/p'

Where $$ --> Is the process name whose Process ID you want to get.

Hope that helps

Cheers,
Kunal

I think you're seeing two due to your invocation method. One method that would be more "tight" would be to have your process write it's PID to a PID file, or have the script write it if necessary. Then check to see if the PID is running, rather than guessing by checking for the existence of a file. ABEND-ing programs rarely clean up after themselves. PID's are either in use or not. The only possible hole in this logic is that PID's are reused periodically as the PID integer is incremented through it's max. The likelihood of that error is quite very very low.

You're seeing two processes because of the way cron runs jobs. It uses 'sh', so the "sh -c" is the actual process that cron has run. However, your script probably starts with "#! /usr/bin/ksh" so you are seeing the second process. A way around this, as has been suggested by other posters, is to have your script echo its current pid to a file. Something like "echo $$ > /path/to/somefile" will probably be enough. That allows you to check your own pid anytime you want.

One additional note, adding to the astute comments by my esteemed colleague blowtorch. Try looking at "exec", which makes the parent's start of the child process fatal.