Subject: Re: Should programs clean-up their /var/run/*.pid files?
To: None <scottr@Plexus.COM>
From: Andrew Brown <codewarrior@daemon.org>
List: current-users
Date: 06/06/1997 16:41:58
>> um...sh forks when it runs [ from bin, or did you forget?
>
>He suggested:
>
>> >kill -0 `cat /var/run/xntpd.pid` && { ... }
>
>which doesn't use [, and hence doesn't fork twice when once will do.

if we're trying to reduce the number of forks in a shell script (which
is something i've never really been bothered with)...

1) from paul goyette
   if [ `ps -ax | grep -c " xntpd " -gt 1 ]; ...
      3 1         2

sh here forks first at 1 to do the backtick subshell, then again at 2
to set up the pipeline, and finally at 3 to do the test.

2) from brian cully
   kill -0 `cat /var/run/xntpd.pid` && { ... }
   2       1

this one only forks twice (at 1 to do the subshell and at 2 to do the
kill) but leads to nasty syntax that i don't really like and is not
all that different from my solution.

3) from andrew brown :)
   if ( kill -0 `cat /var/run/xntpd.pid` 2>/dev/null ); ...
      1 3       2

here it forks first (i might have this order wrong) to do the paren
subshell (which i have since determined i don't need since i can
redirect stderr to /dev/null with out it), at 2 to determine the pid
from the file (which you can easily get in two steps by using read
redirected in from the file) and at 3 to do the kill.  if you go to
the two step version, then you can get it down to one fork, yes?

   read pid < /var/run/xntpd.pid
   if kill -0 $pid 2>/dev/null
   then
   ...

one fork, at the kill, yes?

(now i'm finished beating a dead horse :)

-- 
|-----< "CODE WARRIOR" >-----|
andrew@echonyc.com (TheMan)        * "ah!  i see you have the internet
codewarrior@daemon.org                               that goes *ping*!"
warfare@graffiti.com      * "information is power -- share the wealth."