Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python daemon no pidfile

Tags:

python

daemon

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()
like image 410
Martino Dino Avatar asked Jul 28 '13 15:07

Martino Dino


1 Answers

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.

like image 144
HeyWatchThis Avatar answered Nov 18 '22 01:11

HeyWatchThis