Python daemon no pidfile

▼魔方 西西 提交于 2019-12-12 11:28:19

问题


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()

回答1:


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.



来源:https://stackoverflow.com/questions/17909754/python-daemon-no-pidfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!