Technical article 125689

Using the ps command within a shell script results in two or more instances of the script being displayed in the output.

OpenServer 5.0.2OpenServer 5.0.4OpenServer 5.0.5OpenServer 5.0.6OpenServer 5.0.7UnixWare 7.1.1UnixWare 7.1.2UnixWare 7.1.3

Written · Last revised

Problem

I have a shell script called "myscript" which contains the following check to see how many instances of the script itself are running:

ps -ef | grep myscript | grep -v grep

when I run this script--and there are no other instances of it running on the system--I might expect to see single line of ps output, as in

$ ./myscript
myuser 21902 21668  3 15:47:37   ttyp1    00:00:00 ./myscript

However, sometimes myscript yields two lines of ps output, as in

$ ./myscript
myuser 21872 21668  2 15:44:52   ttyp1    00:00:00 ./myscript
myuser 21873 21872  2 15:44:52   ttyp1    00:00:00 ./myscript

where one instance of the script is a child of the other. At times, it is possible to actually see three occurrences of "myscript" appearing in the ps output.

I would like to use this command sequence (ps -ef|grep|grep -v)
to detect how many instances of the script are running, but
either the ps output is unpredictable, or there is a bug which
creates multiple instances of the script, where I have only
really invoked it a single time.  What can be done about this?

Cause

This is not a bug, but is merely a phenomenon caused by the fact
that the shell must fork a copy of itself each time before it
executes a command specified in script.  In the case of
"myscript", the original shell (i.e., the "parent" copy of
myscript) must fork several processes in order for the
piped sequence "ps -ef | grep myscript | grep -v grep" to be
executed.  Because this forking is serialized (the degree of
serialization depends somewhat on what kind of shell and what
version of the OS is being used), there is a narrow window of
time during which the ps process executes while one or more of the
"grep" processes has not begun both fork and exec yet.  The net
result is that a forked process which eventually execs grep
can still be identified as a copy of "myscript" at the time
that ps runs.  Hence there can be two or even three instances
of "myscript" showing up in ps output.

Solution

Do not use the above piped sequence to accurately and consistently
detect the number of instances of the running script.  The fact
that using the ps command in a pipeline can generate such
varied results suggests that usage of the ps command should
be decoupled from any pipes that are subsequently used, or that
the ps command should not be used altogether.
Consider the following alternatives:

1) Use ps with a temporary file, for example,

ps -ef > /tmp/foo.$$
PCOUNT=`grep myscript /tmp/foo.$$ | wc -l`
OR
2)
  PCOUNT=`fuser myscript-pathname 2> /dev/null | awk 'END {print NF}'`

Here, PCOUNT gives the number of occurrences of the running script.

The first alternative eliminates the timing window by not placing
the ps command in a pipeline at all.  We also don't need
"grep -v grep" as a result.
The second alternative eliminates the ps command altogether,
although it must be run as root because it utilizes the fuser
utility.  This utility lists process IDs that use the file pathname
listed as the argument.  In our case, "myscript-pathname" is
the full path to myscript, and the small awk script filters out
the number of processes using that path.  Because fuser operates
differently depending on whether a file is an executable
script or not, it is more robust than the previous methods.
For instance, suppose someone is doing "vi myscript" when myscript
is run.  The above methods which use ps cannot distinguish
(unless more specific parsing is employed) between processes
which run the script and those which edit the script, and
will return an incorrect result.  However, fuser will only detect
this if the file happens to be open (the majority of the time,
it is not).   If the file _is_ open, fuser will put a code
next to the PID ("o" as opposed to "t"), and the awk script can
be further refined to just search for PIDs with "t" appended to
indicate that the file path used by the PID is an instance of
an executable text image.

From the Xinuos technical article database, record 125689. Reproduced with permission. The original is at www.sco.com/ta/125689, which is reachable but blocked from search engines.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.