On Sat, 2008-09-06 at 10:20 +1000, Cameron Simpson wrote:
Going from reliable (trust the pid file) to the more cautious (trust
the
pid file, but check the pid has not been recycled (daemon dies without
tiying up the pid file, new unrelated process gets the same pid)) you
can do something like this:
if [ -s /var/run/the-pid-file.pid ]
then
pid=`cat /var/run/the-pid-file.pid`
if [ -n "$pid" ]
then
pidcmd=`ps -p "$pid" -o command | awk '{print $1}'`
if [ "x$pidcmd" = 'xyour-java-command' ]
then
kill "$pid"
fi
fi
fi
i.e. check that the pid is still associated with the original daemon.
If you want to be picky, this is actually not enough. It just tells you
that process $pid is currently running a program with the same name as
the one you want. It may even be the same executable file, but it is not
necessarily the same process as before. Very unlikely, and it may not
matter to you, but it's worth being aware of.
One way round it might be to compare the file creation time
of /var/run/the-pid-file.pid with /proc/<pid>/starttime (some fiddly
arithmetic required here). Clearly the pid file should to be younger
than the process.
poc