Avoiding 'sh -c' when running ps from CRON

Hi,
I have a script which has the below line:

ps -ef | grep ${SCRIPT_NAME} | grep ksh | grep -v grep >> /tmp/instance.tmp

When the script is invoked through CRON, I get 2 lines in instance.tmp when actually only one instance is running:

    cdrd 17790 17789  0 15:14:01 ?        0:00 /bin/ksh /scripts/dispatch.ksh CLIENT.cfg
    cdrd 17789   191  0 15:14:01 ?        0:00 sh -c /scripts/dispatch.ksh CLIENT.cfg 2>/dev/null >/de

Why is the 'sh -c' entry getting displayed in ps -ef ? Is there anyway to avoid the 'sh -c' entry from the output of 'ps -ef' ?
I could always use 'grep -v "sh -c"', but I am looking to avoid grep.

Please help.

Thanks in advance.

this line:

cdrd 17789 191 0 15:14:01 ? 0:00 sh -c /scripts/dispatch.ksh CLIENT.cfg 2>/dev/null >/de

is the parent process of this line:

cdrd 17790 17789 0 15:14:01 ? 0:00 /bin/ksh /scripts/dispatch.ksh CLIENT.cfg

There are usually better ways to find running scripts. What OS are you on?

It's the way cron itself runs a cron line.

Your "grep ksh" is finding the string "ksh" in the name of the script.

If you are trying to prevent two occurances running at the same time, this is better achieved by using a flag file. Beware that the unix "ps" command can sometimes miss processes if the kernel is under stress.

I am using the Sun solaris OS.

So if flag file the only option I can look forward to ?
Please suggest if there is any other option along with ps that will prevent 'sh -c' from being fetched..

There is no solution which will stop cron from using sh -c . It is the way cron works. When you post a crontab take note of the message you receive.

Ps: "I am using the Sun solaris OS"
This could be almost any version of a Berkeley Unix or Unix System V Operating System amongst the Sun (now Oracle) hybrids:
Solaris (operating system) - Wikipedia, the free encyclopedia

Use the uname -a command and blot anything confidential like computer names with X's.

Interrogating "ps -ef" with "grep" is not the best way to determine if another instances are running (and it is unreliable). Use semaphore files.
In your context if you really want to use ps , then ps -fu<username> reduces the possibilities for mismatches (but sometimes ps will still omit processes).

Ps. The simple answer to your question is to use grep -v but you have rejected this for some reason.

1 Like

Thanks methyl.

My uname result is:
SunOS xxxxxx Generic_118558-02 sun4u sparc SUNW,Sun-Fire-V240.

Can you please detail on the semaphore method that you talked about ?
I could use that probably if the 'ps' is unreliable as you suggest.

Thanks.

try like this :wink:

ps -ef | grep ${SCRIPT_NAME} | grep [^.]ksh | grep -v grep >> /tmp/instance.tmp
1 Like

In the semaphore method, the script first checks for the presence of a semaphore file and exits if the file is present. If the file is not present the script immediately creates the semaphore file and subseqently deletes the semaphore file on exit.
Some people call this an interlock file (or even a mutex).

This semaphore file also gives you a reference for when the script started so a monitoring process could detect an overrunning process.

Hi Everyone,
I found a method to avoid the 'sh -c' getting displayed in ps result when run in CRON.

The below line works:
ps -d -f | grep ${SCRIPT_NAME} | grep ksh | grep -v grep >> /tmp/instance.tmp

Using a '-d' as an option with ps blocks the 'sh -c' from getting fetched.

Thanks a lot for all the help.

I would also try to use the semaphore method as suggested by methyl.