我有循环的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”运行它时,它会暂停一段时间,然后用这个meesage取消:
警告: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...
答案 0 :(得分:3)
有关更改磁盘的错误意味着文件已更改。
运行systemctl daemon-reload
后,将重新读取该文件,并且您将能够启动该服务。
您可以使用this manual pages中所述的通知。服务的类型是notify
。
接下来是:您将服务类型称为forking
。你的流程真的有问题吗?如果您使用分叉,建议设置PIDfile
选项。
使用systemd,没有必要将进程分叉为守护进程。