Search code examples
unixdaemonpid

Best practice: PID file for unix daemon


Is it considered best practice for the parent or child process for a Unix daemon to write the PID file (for the child).


Solution

  • 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.