Inconsistent output from "ps" when running jobs from crontab

Inconsistent output from "ps" when running jobs from crontab

Post by rluke » Wed, 04 May 2005 00:53:17


I have a ksh script named "zxTest" running on an HP-UX B.11.11
multi-CPU server. I am executing the script every 5 minutes from a
crontab which looks like (some_parms and some_path are actually
something else...):

0,5,10,15,20,25,30,35,40,45,50,55 * * * * . $HOME/
some_parms >/dev/null; ksh -c '/some_path/'

Eventually will be doing some job scheduling, but currently
it does absolutely nothing. However, the first thing that
does is check if it is already running, and exits if it is. The check
is implemented with the following code:

l_process_count=`ps -u ${LOGNAME} -x | grep "zx[T]est" | grep -c -v
if [[ ${l_process_count} -gt 1 ]]
# exit immediately

Periodically, the program reports that it is already running, but I
know that this isn't really the case. So I started logging the output
from the ps command and occasionally it reports the following (2-3
times per day, running every 5 minutes):

Sun May 1 12:10:00 CDT 2005
userx 24687 1330 2 12:15:00 ? 0:00 sh -c . $HOME/
some_parms >/dev/null; ksh -c '/some_path/'
userx 24752 24722 2 12:15:00 ? 0:00 /bin/ksh /some_path/
userx 24722 24687 3 12:15:00 ? 0:00 /bin/ksh /some_path/
Sun May 1 12:15:00 CDT 2005

Could someone help explain why "ps" is showing this extra process in
this inconsistent way? Is ksh spawning an extra sub-shell? Can we
eliminate this extra sub-shell, or alternatively, make it appear
consistently (therefore exit only if process_count greater than 2)?

Also, is there a quick fix to make this "cheap and dirty"
"already-running?" check more reliable? I am trying to avoid a complex
locking scheme. The check does not have to be 100% reliable, but 2-3 of
these per day seems too high.

Thanks in advance,
--Richard Lukes