5

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?

1 Answer 1

2

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:

  • Use daemon.DaemonRunner (we have found this very convenient to use)
  • Change the type of pidfile to a daemon.pidfile.TimeoutPIDLockFile.
Sign up to request clarification or add additional context in comments.

2 Comments

I tried the 2nd approach and that is given even worse results. There is no pid file in the working directory and it is still complaining about its existence! I confirmed via strace
strace gives "open("test.pid", O_WRONLY|O_CREAT|O_EXCL, 0644) = -1 EEXIST (File exists)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.