Detecting running processes... this one's portable

Peter S. Shenkin peters at cubsvax.UUCP
Fri Aug 1 00:15:52 AEST 1986


In article <ncoast.1325> allbery at ncoast.UUCP (Brandon Allbery) writes:
>I have another method; it works everywhere except that under System V it
>doesn't work to see if a process has exited and should be waited upon (this
>doesn't apply to an ``after'' program anyway):
>--------
>#! /bin/sh
>while kill -0 ${1-?}; do
>	sleep ${2-60}
>done
>--------
>The C version is just as trivial....

This doesn't work under ULTRIX or, I would guess, 4.2.  "kill -0" returns the
error message, "bad signal number" regardless of the existence of the process.

In addition to what's been posted on the net, I've received a number of mail
followups to my original posting, most of contained good ideas for 
improving the routine.  Hopefully I'll get around to putting together and 
posting a "master version" eventually...  but for now, a change I've made in
the version I'm using is to replace the use of both sed and awk with a simple
"ps x pid#", followed by a test of $status.  If the process existed, $status
is 0;  else, 1.  This is better than another suggestion, involving the line,
"ps x | fgrep pid# | fgrep -v fgrep", for three reasons.  First of all, it's
simpler.  Second, the fgrep method will often find strings you're not looking
for:  like job number 22299 when you're looking for job 222 (I suppose you
could specify  -w in the first fgrep).  Third, "after" will always find itself;
that is, if pid# 3998 was the command "after 3997 script1", the "after" line
will be found by the fgrep string, and script1 will never execute.  To take
care of this, you have to pipe through an "fgrep -v -w after".....   and now
that I've seen all this I wonder if there is not yet another tiger lurking in
these woods....

The "kill -0" idea was sent to me by mail... was it by you, Brandon?  (I'm too
lazy to look right now!)  Whoever it was, I tried to reply by mail, but the 
letter bounced.

But this all concerns the general question, "what is the most efficient 
portable UNIX method for checking whether a process exists, given a pid#".
In fact, it would be good enough to answer four questions:  what is the most
efficient way to do this:
	1) in bsd systems, from the shell
	2) in bsd systems, from a C-program
	3) in at&t systems, from the shell
	4) in at&t systems, from a C-program.

Peter S. Shenkin	 Columbia Univ. Biology Dept., NY, NY  10027
{philabs,rna}!cubsvax!peters		cubsvax!peters at columbia.ARPA



More information about the Comp.sources.unix mailing list