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
:
- 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
Listening for New Processes in Linux Kernel Module
Removing Parts of a String That Contain Digit with Sed/Perl
Accessing Apache2 Residing Inside Docker Container from Host Machine Web Browser
Install Opencl(Amd Sdk Kit) on Linux Without Root Privilege
Installing Zlib in Linux Server
How to Include Cutil.H in Linux
Glib: G_Atomic_Int_Get Becomes No-Op
Linux Slab Allocator and Cache Performance
Libcurl with Libssh2 - One or More Libs Available at Link-Time Are Not Available Run-Time
"Segmentation Fault (Core Dumped)" Error in Fortran Gfortran Linux
Cannot Compile Mergevec.Cpp from Haartraining Tutorial
How to Send Integer with Message Queue with Posix API in Linux
How to Upgrade a Package in Linux That Was Built from Source
Boost Asio with Ecdsa Certificate Issue
How to Use Xargs to Run a Function in a Command Substitution for Each Match
How to Join 2 CSV Files with a Shell Script
How to Store Multiple Pats/Passwords for Use by Git
How to Replace The Word "Hello" with "Goodbye" in Every File in This Directory, and Also Recursively