![我如何告诉系统该服务已启动?](https://linux22.com/image/1070088/%E6%88%91%E5%A6%82%E4%BD%95%E5%91%8A%E8%AF%89%E7%B3%BB%E7%BB%9F%E8%AF%A5%E6%9C%8D%E5%8A%A1%E5%B7%B2%E5%90%AF%E5%8A%A8%EF%BC%9F.png)
我想将 Python 脚本作为服务运行,脚本启动并运行,但系统显示它处于激活状态,这就是它不断重启的原因
systemd[1]: rpi-rf.service: Failed with result 'timeout'.
如何告诉系统该脚本已启动并正在运行?.service:
# systemd unit file for the Python Demo Service
[Unit]
# Human readable name of the unit
Description=Python rpi-rf_receiver
[Service]
# Command to execute when the service is started
ExecStart=/usr/bin/python3 /usr/local/lib/rpi-rf/rpi-rf_receive
# Disable Python's buffering of STDOUT and STDERR so that output from the
# service shows up immediately in systemd's logs
Environment=PYTHONUNBUFFERED=1
# Automatically restart the service if it crashes
Restart=on-failure
RestartSec=5
# Our service will notify systemd once it is up and running
Type=notify
# Use a dedicated user to run our service
User=root
[Install]
# Tell systemd to automatically start this service when the system boots
# (assuming the service is enabled)
WantedBy=default.target
答案1
from systemd.daemon import notify, Notification
# Send READY=1
notify(Notification.READY)
答案2
在 systemd 中添加启动项比较复杂,比较麻烦。为了方便使用,有一个工具添加服务提供了一种在 systemd 中快速添加启动项的简单方法。
pip3 install add_service
python -m add_service [shell_file/cmd] [user (default `whoami`)]
例子:
python -m add_service ssh_nat.sh # by default user is `whoami`
python -m add_service "`which python3` -m http.server 80" root