![保持 systemd 服务启动的进程处于活动状态时出现问题](https://linux22.com/image/200844/%E4%BF%9D%E6%8C%81%20systemd%20%E6%9C%8D%E5%8A%A1%E5%90%AF%E5%8A%A8%E7%9A%84%E8%BF%9B%E7%A8%8B%E5%A4%84%E4%BA%8E%E6%B4%BB%E5%8A%A8%E7%8A%B6%E6%80%81%E6%97%B6%E5%87%BA%E7%8E%B0%E9%97%AE%E9%A2%98.png)
我正在努力让我的第一项systemd
服务按预期工作。这是我所拥有的:
gateway-watchdog.service
:
# A systemd service for the Gateway Watchdog
[Unit]
Description=gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
[Service]
Type=simple
User=root
WorkingDirectory=/home/ubuntu/lora_gateway/
ExecStart=/home/ubuntu/lora_gateway/watchdog.sh
[Install]
WantedBy=multi-user.target
gateway-watchdog.timer
:
[Unit]
Description=gateway-watchdog: Timer to run the associated gateway-watchdog service
[Timer]
# Run service 1 minute after boot
OnBootSec=1min
# Run service 15 minutes after the last run
OnUnitActiveSec=30s
[Install]
WantedBy=timers.target
watchdog.sh
有这些行(包含在一些if
语句中):
#!/bin/bash
...
if [[condition]]; then
command="python3 gateway.py"
# Process needs to be run in the background with '&'. If it isn't, the systemd service will treat this script
# as still running and so not re-run it at the time specified by the timer service.
echo "2 $(date +'%F %T'): Running: $command" >> $LOG_FILE_PATH
eval "$command"
# Command must be run using eval. If it isn't, the ampersand at the end of the command is effectively ignored
exit
fi
我需要的:
该服务将运行watchdog.sh
。如果if
脚本中的条件为真,watchdog.sh
则将python3 gateway.py
作为单独的进程启动,然后退出,并且该服务将不再被视为活动状态,因此它将在 30 秒后再次运行。
通过不同服务的各种组合Type
(我已经尝试过simple
、oneshot
和forking
),在和/或我的命令行&
末尾使用,和/或使用,我可以启动并保持活动状态,或者让服务运行30 秒,但不能两者兼而有之。ExecStart
nohup
gateway.py
当python3 gateway.py
保持活动状态时,它始终是服务的一部分CGroup
,因此服务会保留active (running)
。
我怎样才能改变这种行为以实现我想要做的事情?任何想法都将不胜感激。
编辑:我已经更新了上面的文件内容,以反映根据 U 的建议所做的更改。
根据上面给出的文件,sudo systemctl status gateway-watchdog.service
给出:
● gateway-watchdog.service - gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
Loaded: loaded (/etc/systemd/system/gateway-watchdog.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2022-01-13 01:45:25 UTC; 8s ago
TriggeredBy: ● gateway-watchdog.timer
Main PID: 30684 (watchdog.sh)
Tasks: 3 (limit: 4435)
CGroup: /system.slice/gateway-watchdog.service
├─30684 /bin/bash /home/ubuntu/lora_gateway/watchdog.sh
└─30696 python3 gateway.py
Jan 13 01:45:25 ubuntu systemd[1]: Started gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running.
好:gateway.py
进程保持运行。错误:服务 30 秒后不会再次运行,因为之前的运行仍然处于活动状态。
如果我把&
后面放入command="python3 gateway.py &"
,sudo systemctl status gateway-watchdog.service
给出:
● gateway-watchdog.service - gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running
Loaded: loaded (/etc/systemd/system/gateway-watchdog.service; enabled; vendor preset: enabled)
Active: inactive (dead) since Thu 2022-01-13 01:49:05 UTC; 6s ago
TriggeredBy: ● gateway-watchdog.timer
Process: 33724 ExecStart=/home/ubuntu/lora_gateway/watchdog.sh (code=exited, status=0/SUCCESS)
Main PID: 33724 (code=exited, status=0/SUCCESS)
Jan 13 01:49:05 ubuntu systemd[1]: Started gateway-watchdog: Watchdog to ensure that Gateway (gateway.py) is running.
Jan 13 01:49:05 ubuntu systemd[1]: gateway-watchdog.service: Succeeded.
好:该服务处于非活动状态,因此 30 秒后它会再次运行。坏:python3 gateway.py
进程立即终止。
答案1
我通过参考找到了解决方案https://stackoverflow.com/a/57041270/2909854。现在command
的行是watchdog.sh
:
command="systemd-run --scope -E setsid nohup python3 $GATEWAY_SCRIPT_PATH"
我还必须将我的服务Type
集更改为forking
,并添加TimeoutSec=1
.不确定超时是否必要,但至少在调试时有帮助。