Killing a Daemon Using a Pid File

Best practice: PID file for unix daemon

The actual daemon process (the child).

According to the daemon man page provided by the systemd package and viewable on a RHEL 7 (or CentOS 7) host by running man daemon:


  1. In the daemon process, write the daemon PID (as returned by getpid())
    to a PID file, for example /run/foobar.pid (for a hypothetical daemon
    "foobar") to ensure that the daemon cannot be started more than once.
    This must be implemented in race-free fashion so that the PID file is
    only updated when it is verified at the same time that the PID
    previously stored in the PID file no longer exists or belongs to a
    foreign process.

You can also read the man page on the internet.

PID files hanging around for daemons after server restart

Use flock (or lockf) on your pidfile, if it succeeds, you can rewrite the pidfile and continue.

This SO answer has a good example on how this is done.

Paster daemon won't shut down because can't read own pid file

From paste script's source code(serve.py), in the PID reading method:

pid = read_pidfile(pidfile)
if pid:
try:
os.kill(int(pid), 0)
return pid
except OSError, e:
if e.errno == errno.EPERM:
return pid
return None

On POSIX-compatible platforms, specifying 0 as a signal just checks whether the process is alive.

However, on Windows, there is no kill system call; Python will use TerminateProcess instead. Remove the os.kill line from paster script or use a POSIX-compatible platform, like Cygwin (POSIX layer on top of Windows) or Linux.



Related Topics



Leave a reply



Submit