我创建了一个 systemd 启动脚本,它只是调用一个带有启动或停止参数的 python 脚本。如果从 shell 手动运行,该脚本可以工作,但是当我运行 systemctl start foo 或 systemctl stop foo 时,它不起作用
[Unit]
Description=foo Backends
After=network.target remote-fs.target nss-lookup.target
[Service]
ExecStart=/opt/foo/scripts/init_backends.py start
ExecStop=/opt/foo/scripts/init_backends.py stop
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
请注意,从 shell 调用 /opt/foo/scripts/init_backends.py start 就可以了
答案1
使用以下设置时它开始工作。我认为 StandardOutput=tty 是关键
[Unit]
Description=foo Backends
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/opt/foo/scripts/init_backends.py start
ExecStop=/opt/foo/scripts/init_backends.py stop
User=root
Group=root
TimeoutSec=300
StandardOutput=tty
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target