我很久以前制作了一些自定义的systemd服务,它们都有相同的配置(当然ExecStart除外)
这个配置已经运行了好几年了,我从18.04 LTS版本开始使用ubuntu,并且一直正常运行。但现在看起来其中一些systemd服务根本没有在启动时启动,配置如下(myapp.service):
[Unit]
Description="myapp"
After=syslog.target network-online.target
Wants=network-online.target
[Service]
Restart=always
RestartSec=10
User=root
Group=root
WorkingDirectory=/opt/myapp
ExecStart=/usr/local/bin/myapp
KillMode=control-group
[Install]
WantedBy=multi-user.target
服务已启用:
$ sudo systemctl enable myapp
Created symlink /etc/systemd/system/multi-user.target.wants/myapp.service → /lib/systemd/system/myapp.service.
如果在重新启动后执行“systemctl status myapp”:
● myapp.service - "myapp"
Loaded: loaded (/lib/systemd/system/myapp.service; enabled; vendor preset: enabled)
Active: inactive (dead)
如果我在重启后运行“journalctl -u myapp -f”命令:
Jan 13 12:10:06 myhost systemd[1]: Started myapp.
Jan 17 07:15:03 myhost systemd[1]: Stopping myapp...
Jan 17 07:15:09 myhost systemd[1]: Stopped myapp.
我的配置有什么问题?
如果我手动启动 /usr/local/bin/myapp,脚本不会出现任何错误,而且我也尝试使用 tmux 运行它,现在已经在后台运行了 3 天,没有出现错误。但是 systemd 在重新启动后就无法启动它。
今天,我还尝试安装一个需要 systemd 配置的新服务,该软件包是 zram-config,默认情况下在启动时启用。 但在执行 apt install zram-config && sudo reboot 命令后:
$ sudo systemctl status zram-config
● zram-config.service - Initializes zram swaping
Loaded: loaded (/lib/systemd/system/zram-config.service; enabled; vendor preset: enabled)
Active: inactive (dead)
但是如果现在我执行以下操作:
$ sudo systemctl start zram-config
$ sudo systemctl status zram-config
● zram-config.service - Initializes zram swaping
Loaded: loaded (/lib/systemd/system/zram-config.service; enabled; vendor preset: enabled)
Active: active (exited) since Mon 2020-01-27 12:25:55 CET; 1s ago
Process: 5541 ExecStart=/usr/bin/init-zram-swapping (code=exited, status=0/SUCCESS)
Main PID: 5541 (code=exited, status=0/SUCCESS)
Jan 27 12:25:55 myhost systemd[1]: Starting Initializes zram swaping...
Jan 27 12:25:55 myhost init-zram-swapping[5541]: Setting up swapspace version 1, size = 985,7 MiB (1033568256 bytes)
Jan 27 12:25:55 myhost init-zram-swapping[5541]: nessuna etichetta, UUID=4ac5c2cd-0c68-4f6d-a5c0-d8f91a509c71
Jan 27 12:25:55 myhost init-zram-swapping[5541]: Setting up swapspace version 1, size = 985,7 MiB (1033568256 bytes)
Jan 27 12:25:55 myhost init-zram-swapping[5541]: nessuna etichetta, UUID=83a4f201-d591-4222-89a6-5bc5aebedef4
Jan 27 12:25:55 myhost init-zram-swapping[5541]: Setting up swapspace version 1, size = 985,7 MiB (1033568256 bytes)
Jan 27 12:25:55 myhost init-zram-swapping[5541]: nessuna etichetta, UUID=1f6f742e-6fb8-4332-b226-bf6918f7ee28
Jan 27 12:25:55 myhost init-zram-swapping[5541]: Setting up swapspace version 1, size = 985,7 MiB (1033568256 bytes)
Jan 27 12:25:55 myhost init-zram-swapping[5541]: nessuna etichetta, UUID=a5509c55-46f5-4112-8fe1-68171f31409e
Jan 27 12:25:55 myhost systemd[1]: Started Initializes zram swaping.
我真的不太明白为什么我的Ubuntu安装中出现了systemd的问题,是不是最好重新安装整个操作系统呢?
谢谢。