ps -ef failing sometimes

Hi Everyone,

we have a shell script "DLP_recv.sh" that has below command which is supposed to return the number of active instances of itself, which means of there is no other instance then commad would return 1 (for the current instance). The problem is that it sometimes it returns 0 which is unlikely because it should count its own instance. This happens rarely, does anyone have any idea, thanks in advance.

job_pattern="$0"
ps -ef|grep "${job_pattern}"|grep -v "grep ${job_pattern}" > /tmp/DLP_recv.$$

Regards.

Most man pages for ps such as this one say

ps is scanning a table that can change very fast during the scan. It's especially bad on a multiple cpu system. It can never be 100% correct.

ok, but the ps is executed within the program when that instance is still active, there is futher processing in the program after the ps is issued so it should return 1.

Thanks.
Rahul.

As posted the script will never return a result because the name of the script does not normally appear in "ps -ef".
It might work if the script was invoked with "sh scriptname" such that the name of the script appears in memory.

This sort of application is best handled with flag files.

The output from "ps" is not reliable. It will sometimes miss processes on a busy system. I've seen this many times with monitoring scripts.