Python systemd服务在启动后不久停止
我尝试将Python脚本作为系统服务运行,但服务没有启动。以下是我的配置:Python systemd服务在启动后不久停止,python,linux,systemd,systemctl,Python,Linux,Systemd,Systemctl,我尝试将Python脚本作为系统服务运行,但服务没有启动。以下是我的配置: pyntp.service: [Unit] Description=Python NTP Service After=multi-user.target [Service] Type=forking ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py [Install] WantedBy=multi-user.target ntpservice.py: #!/usr/
pyntp.service
:
[Unit]
Description=Python NTP Service
After=multi-user.target
[Service]
Type=forking
ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py
[Install]
WantedBy=multi-user.target
ntpservice.py
:
#!/usr/bin/python
import os
import time
import json
pid = os.fork()
if pid == 0:
print 'parent'
else:
print 'child'
while True:
print('123')
time.sleep(1)
启动服务的步骤如下所示:
cp pyntp.service /etc/systemd/system/
cp ntpservice.py /usr/local/bin/
systemctl daemon-reload
systemctl enable pyntp.service
systemctl start pyntp.service
问题是,当我试图查看pyntp服务的状态时,总是这样:
● pyntp.service - Python NTP Service
Loaded: loaded (/usr/lib/systemd/system/pyntp.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Wed 2018-11-14 22:27:56 CST; 34min ago
Process: 801 ExecStart=/usr/bin/python $HOME/ntp/ntpservice.py (code=exited, status=0/SUCCESS)
Main PID: 801 (code=exited, status=0/SUCCESS)
Nov 14 22:27:56 HES1 systemd[1]: Started Python NTP Service.
Nov 14 22:27:56 HES1 systemd[1]: Starting Python NTP Service...
谁能帮我解决这个问题?谢谢。您的程序运行正常。仅仅
fork
ing不足以创建守护进程。发生的情况是,只要父进程运行,代码就一直在运行,然后(两个分支)在父进程终止时退出。您想要的是编写一个守护进程(并让它由systemd控制)。您可能会发现这个问题有助于解释一些简单的方法:
fork
是这个过程的一个重要部分,但是仅仅做一个fork
本身并不能完全解决问题。如果您想了解如何使用fork
手动监控流程的更详细示例,可以阅读以下内容: