我想将 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