6

Hello I'm writing a daemon in python which uses the python-daemon module, my application starts correctly, there is a pidfile.lock created but no sign of the pidfile containing the process id.

import daemon
import lockfile

import perfagentmain

context = daemon.DaemonContext(
    working_directory='/opt/lib/perf-agent',
    umask=0o002,
    pidfile=lockfile.FileLock('/var/run/perf-agent.pid')
    )


with context:
    perfagentmain.start()
Martino Dino
  • 585
  • 6
  • 14
  • 3
    `pidfile = daemon.pidlockfile.PIDLockFile("/var/run/zebra.pid")` — worked for me. See http://stackoverflow.com/a/13082597/159149 – koddo Feb 21 '14 at 12:53

1 Answers1

9

I agree with @npoektop 's comment about the solution. I would just say that daemon.pidlockfile does not exist at the time I am writing this. daemon.pidfile instead. Maybe that's a recent name change?

So instead, here's the general solution using the daemon.pidfile module instead of the lockfile module.

import daemon
import daemon.pidfile
import perfagentmain

context = daemon.DaemonContext(
    working_directory='/opt/lib/perf-agent',
    umask=0o002,
    pidfile=daemon.pidfile.PIDLockFile('/var/run/perf-agent.pid')
    )

with context:
    perfagentmain.start()

And @Martino Dino, you're absolutely right, it seems the lockfile module has a totally different implementation of writing lock files. (even though python-daemon actually requires lockfile)

When I tried out pidfile = lockfile.FileLock('/var/run/mydaemon.pid') for my own needs, I instead saw a file called <MY_MACHINE_NAME>-<8CHAR_HEX_ID>.<PID_OFF_BY_2>, along with a file /var/run/mydaemon.pid.lock . This answer mentions how this method of hard linking a randomly named file to your pidlock file was a file-locking method prior to the use of the O_EXCL flag used when opening files.

But the annoying part was that the file did not contain the PID as you said, and the file name had a PID which was off by a few numbers of the correct PID, so it was terribly misleading.

Community
  • 1
  • 1
HeyWatchThis
  • 21,241
  • 6
  • 33
  • 41