如何让 Systemd 服务永远运行(“启动尚未完成。请稍后再试。”)

Bas*_*asj 6 systemd services

我创建了一个必须永远运行的systemd服务(因为它在我的嵌入式计算机上完成主要工作):

# /etc/systemd/system/samplerbox.service
########################################
[Unit]
Description=Starts SamplerBox

[Service]
Type=oneshot
ExecStart=/root/SamplerBox/samplerbox.sh
WorkingDirectory=/root/SamplerBox/

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

这是它的实际作用:

# /root/SamplerBox/samplerbox.sh
################################
#!/bin/sh
python /root/SamplerBox/samplerbox.py
Run Code Online (Sandbox Code Playgroud)

我启用了这项服务

systemctl enable /etc/systemd/system/samplerbox.service
Run Code Online (Sandbox Code Playgroud)

它可以工作,并且在启动时启动。


但是,由于我启用了这项服务,所以当我这样做时systemd-analyze,我看到:

Bootup is not yet finished. Please try again later.
Run Code Online (Sandbox Code Playgroud)

此外,我得到的信息表明该服务仍被视为“激活”/启动:

# systemctl status samplerbox
â samplerbox.service - Starts SamplerBox
   Loaded: loaded (/etc/systemd/system/samplerbox.service; enabled)
   Active: activating (start) since Thu 1970-01-01 00:14:01 UTC; 11min ago
 Main PID: 258 (samplerbox.sh)
   CGroup: /system.slice/samplerbox.service
           ââ258 /bin/sh /root/SamplerBox/samplerbox.sh
           ââ260 python /root/SamplerBox/samplerbox.py
Run Code Online (Sandbox Code Playgroud)

如何正确地使服务永远运行?

Bas*_*asj 6

解决方案很简单:必须更换

[Service]
Type=oneshot
Run Code Online (Sandbox Code Playgroud)

经过

[Service]
Type=simple
Run Code Online (Sandbox Code Playgroud)

该文档指出:

Type=simple(默认):systemd 认为服务会立即启动。进程不能分叉。如果需要在此服务上订购其他服务,请不要使用此类型,除非它是套接字激活的。

...

Type=oneshot:这对于执行单个作业然后退出的脚本很有用。您可能还想设置 RemainAfterExit=yes,以便 systemd 在进程退出后仍然认为该服务处于活动状态。