Python守护进程没有pidfile [英] Python daemon no pidfile
问题描述
你好,我正在使用python-daemon模块在python中编写一个守护程序,我的应用程序正确启动,创建了一个pidfile.lock,但没有包含进程ID的pidfile的迹象。
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()
推荐答案
我同意@npoektop对解决方案的评论。我只是说 daemon.pidlockfile
在撰写本文时不存在。 daemon.pidfile
代替。
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?
因此,这里是使用 daemon.pidfile
模块而不是的常规解决方案。
lockfile
模块。
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()
@Martino Dino,您说的很对,看来 lockfile
模块具有完全不同的写入锁定文件的实现。 (即使 python-daemon
实际上需要 lockfile
)
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
)
当我尝试 pidfile = lockfile.FileLock('/ var / run / mydaemon.pid')
来满足自己的需要时,却看到了一个名为<$ c $的文件c>< MY_MACHINE_NAME>-< 8CHAR_HEX_ID>。< PID_OFF_BY_2> ,以及文件 /var/run/mydaemon.pid.lock
。 此答案提到了这种将随机命名的文件硬链接到pidlock文件的方法是一种文件锁定方法,然后使用 O_EXCL
标志打开文件时使用。
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.
但是令人讨厌的部分是该文件不包含您所说的PID,并且文件名的PID与某些正确的PID 偏离,因此极具误导性。
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.
这篇关于Python守护进程没有pidfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!