I am trying to daemonize a process using the daemon
module. Code looks something like this
import sys
import time
import daemon
import lockfile
def do_things():
while True:
print "hello"
time.sleep(3)
def main()
context = daemon.DaemonContext(stdout=sys.stdout,
pidfile=lockfile.FileLock('test.pid'))
with context:
do_things()
Now here you see that I am creating a lock PID file. Now I run this program and it runs fine. Now to test the PID/daemon functionality I start another instance of the program using
python test.py
Now this time it should NOT run, as a prior instance is already running. Turns out that the 2nd instance start and gets in a loop(this one is not the while
loop in my test function). Running strace
on this 2nd instance gives the following output continously
stat("/some-path-here/[email protected]", {st_mode=S_IFREG|0666,
st_size=0, ...}) = 0
select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
link("/some-path-here/Talha@Fedora14- 4e1a9720.21520",
"/somepath/test.pid.lock") = -1 EEXIST (File exists)
And this trace appears perpetually until the process is forcefully killed. THe lockfile functions have indeed detected the presence of an existing lock file but the problem is the program does not exit. Also i would like this error to be displayed that the pid file already exists.
How can this be done?
NOTE: This answer assumes you are using the python-daemon
library.
The daemon library comes with a helper class daemonDaemonRunner
which handles creating the pid file. Looking at the internals of that, it uses daemon.pidfile.TimeoutPIDLockFile
as the type of lockfile.
So, it looks like you may solve this by either:
daemon.DaemonRunner
(we have found this very convenient to use)daemon.pidfile.TimeoutPIDLockFile
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With