pidofproc function in functions script

Dan Nicholson dbn.lists at gmail.com
Mon Jul 2 12:29:38 PDT 2007


On 7/2/07, dennisjperkins at comcast.net <dennisjperkins at comcast.net> wrote:
>
> I think I looked at the latest version, but I have a printout of the functions file and this is what it has:
>
>                 for pid in ${lpids}
>                 do
>                         if [ "${pid}" -ne "$$" -a "${pid}" -ne "${PPID}" ]; then
>                                 kill -0 "${pid}" > /dev/null &&
>                                 pidlist="${pidlist} ${pid}"
>                         fi
>
>                         if [ "${silent}" -ne "1" ]; then
>                                 echo "${pidlist}"
>                         fi
>
>                         test -z "${pidlist}" &&
>                         # Program is dead, pidfile exists
>                         return 1
>                         # else
>                         return 0
>                 done
>
> Shouldn't done be before the comment line that says the program is dead?

Ahh, OK. I'm glad you brought this up because this part is kind of broken.

1. It sure looks like the test && return on $pidlist should happen
after the for loop. This happens to work right now because the for
loop is only going to ever have one value in $lpids. This is because:

2. lpids=`head -n 1 ${pidfile}`
That's just not right. If the pidfile really would have multiple pids
in it, I would imagine they would be listed like:

/var/run/foo.pid:
1000
1001
...

So, doing head -n1 on that will always just get you one pid unless
they're all listed on a single line. Then doing a for loop is silly. I
was playing with this on my system by changing to something like:

while read pid; do
...
done < $pidfile

but it was broken somehow. Still have to investigate that. So, yeah
the test should come after the for loop, but the for loop needs to be
fixed, too. It might have to have two loops to account for multiple
pids.

while read line; do
  for pid in $line; do
    ...
  done
done < $pidfile

> Maybe you can clarify the first if statement in this loop.  I looked at the few pidfiles on my computer.  None of them seems to indicate a need for $$ and $PPID.  Maybe my system is not complex enough, but why should we ever expect to see them in a pidfile?

No, it's just a sanity check. We're just trying to make sure that the
pid being tested isn't the actual script being run ($$) or it's parent
($PPID) since those would be surely running and definitely don't
represent the process we're trying to test.

If you feel like playing around with that and fixing it to do the
right thing, I'll certainly look it over. I really want to make the
pidfile code more robust.

--
Dan



More information about the lfs-support mailing list