Re: service; ps & grep help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 07Sep2008 02:29, Patrick O'Callaghan <pocallaghan@xxxxxxxxx> wrote:
| 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).

Portable only to other Linux systems. Another thing to be aware of.
But there are ps incantations that recite the process start time, so you
could pull it from there if it's precise enough. Point taken.

| Clearly the pid file should to be younger
| than the process.

True. It's a rather coarse check though.

I would much rather keep a caretaker process around. If our concern is
that the daemon dies, badly enough that it doesn't tidy up its pid file
(probable if the daemon isn't responsible for the pidfile in the first
place:-) then I like this:

  ( start daemon &
    echo $! >/var/run/the-daemon.pid
    wait
    rm /var/run/the-daemon.pid
  ) &

That way the pid file should get removed if the daemon dies. Unless
something takes out the caretaker subshell (requires special effort, or
a disaster:-) the pid file will be reliably removed.

I have an "ifpid" command for performing the pidfile check augumented
with the "ps|grep" check which accepts an arbitrary regexp here:

  http://www.cskk.ezoshosting.com/cs//css/bin/ifpid

This lets one check for command line arguments as well as the command
name. This lets you be a bit more paranoid. I tend to use it like this:

  if ifpid -q /var/run/the-pidfile.pid ' command .* -f config-file'
  then
    do stuff, eg prod daemon to do a log rotate or something
  fi

which makes for easy to read scripts.

Cheers,
-- 
Cameron Simpson <cs@xxxxxxxxxx> DoD#743
http://www.cskk.ezoshosting.com/cs/

[...] if share-dealing had anything to do with future yields, we'd all
have BT shares and stockbrokers would be wise, Yoda-like figures who
don't touch cocaine for fear of the long-term risks. [...]
        - _NTK_, 26jul99 http://www.ntk.net/

-- 
fedora-list mailing list
fedora-list@xxxxxxxxxx
To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
Guidelines: http://fedoraproject.org/wiki/Communicate/MailingListGuidelines

[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux