我有带有循环的 python 应用程序,它生成一些文件,保存视频图像和其他一些东西。我将它安装在 Fedora (17) PC 上并希望它“永远”运行,即如果它挂起(我可以将一些 keep_alive 放入循环文件中) - 它应该重新启动。它也应该在重新启动时启动。据我了解,python-daemon 有助于做到这一点,并且在 Fedora 中使用 systemd。我有以下 systemd 配置文件(我不确定某些参数,因为文档对于我的 linux 知识水平来说太复杂了):
[Unit]
Description=TPR Daemon
[Service]
Type=forking
Restart=always
WorkingDirectory=/home/igor/tpr
PIDFile=/var/run/tpr.pid
ExecStart=/usr/bin/python /home/igor/tpr/testd.py
[Install]
WantedBy=default.target
这是我的testd.py:
import daemon
import time, sys
class MyDaemon(object):
def __init__(self):
pass
def run(self):
while True:
print 'I am alive!'
time.sleep(1)
if __name__ == '__main__':
with daemon.DaemonContext(stdout=sys.stdout):
check = MyDaemon()
check.run()
当我用“sudo systemctl start tpr.service”运行它时,它会挂起一段时间,然后用这个消息取消:
警告:磁盘上的 tpr.service 单元文件已更改,建议使用“systemctl --system daemon-reload”。tpr.service 的作业失败。有关详细信息,请参阅“systemctl status tpr.service”和“journalctl -xn”。
以下是来自 /var/log/messages 的一些日志:
Aug 9 21:32:27 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 9 21:32:27 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 9 21:32:27 localhost systemd[1]: Stopping TPR Daemon...
Aug 9 21:32:27 localhost systemd[1]: Starting TPR Daemon...
Aug 9 21:33:57 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
...
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost python[28702]: I am alive!
Aug 9 21:33:57 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 9 21:33:57 localhost systemd[1]: Failed to start TPR Daemon.
Aug 9 21:33:57 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 9 21:33:57 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 9 21:33:57 localhost systemd[1]: Stopping TPR Daemon...
Aug 9 21:33:57 localhost systemd[1]: Starting TPR Daemon...
所以它应该正在运行,但是这个错误是什么?也许有一些简单方便的方法来完成我的任务,我正在发明自行车?
更新:
似乎守护进程应该以某种方式让 systemd 知道它已经启动..但是如何?
Aug 10 01:15:36 localhost systemd[1]: Starting TPR Daemon...
Aug 10 01:17:06 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 10 01:17:06 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 10 01:17:06 localhost systemd[1]: Failed to start TPR Daemon.
Aug 10 01:17:06 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 10 01:17:06 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 10 01:17:06 localhost systemd[1]: Stopping TPR Daemon...
Aug 10 01:17:06 localhost systemd[1]: Starting TPR Daemon...
Aug 10 01:18:36 localhost systemd[1]: tpr.service operation timed out. Terminating.
Aug 10 01:18:36 localhost systemd[1]: tpr.service: control process exited, code=exited status=1
Aug 10 01:18:36 localhost systemd[1]: Failed to start TPR Daemon.
Aug 10 01:18:36 localhost systemd[1]: Unit tpr.service entered failed state.
Aug 10 01:18:36 localhost systemd[1]: tpr.service holdoff time over, scheduling restart.
Aug 10 01:18:36 localhost systemd[1]: Stopping TPR Daemon...
Aug 10 01:18:36 localhost systemd[1]: Starting TPR Daemon...