我有带有循环的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”运行它时,它会挂起一段时间,然后用以下消息取消:
Warning: Unit file of tpr.service changed on disk, 'systemctl --system daemon-reload' recommended. Job for tpr.service failed. See 'systemctl status tpr.service' and 'journalctl -xn' for details.
这里是来自/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...
最佳答案
有关磁盘上更改的错误意味着文件已更改。
运行 systemctl daemon-reload 后,该文件将被重新读取,然后您就可以启动该服务。
您可以按照 this manual pages 中所述使用通知。 。该服务的类型是notify
。
接下来的事情是:您将服务类型设置为forking
。你的进程真的 fork 了吗?如果您使用 fork ,建议设置PIDfile
选项。
使用 systemd,无需 fork 您的进程即可成为守护进程。
关于python 应用程序(守护进程)和 systemd fedora,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18152503/