在 systemd 下无人值守更新后自动重启服务

Automatically restart a service after unattended updates under systemd

我添加了一项服务(在本例中为 Seafile),我希望 运行 始终使用服务文件将其连接到 systemd。它运行良好,但每次无人值守更新时 运行,服务都会正确关闭 - 但从未重新启动。

服务文件如下所示:

[Unit]
Description=Seafile
Requires=mysql.service
After=mysql.service

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
User=root
Group=root
PermissionsStartOnly=true
ExecStart=/srv/start-seafile
TimeoutSec=600
Restart=on-failure

/srv/start-seafile 看起来像这样:

#!/bin/bash

cd /srv/seafile/XXXX/seafile-server-latest && nohup ./seafile.sh start

就像我说的 - 这完美地工作 - systemd 可以启用/禁用/启动/停止服务,意识到它是否已启动/运行ning - 所以我必须做 something对了。

# systemctl start seafile
# systemctl status seafile
* seafile.service - Seafile
   Loaded: loaded (/etc/systemd/system/seafile.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-01-28 17:53:51 CET; 8s ago
  Process: 6569 ExecStart=/srv/start-seafile (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/seafile.service
       |-6598 /srv/seafile/XXXX/seafile-server-5.1.4/seafile/bin/seafile-controller -c /srv/seafile/XXXX/ccnet -d /volume1/Seafile -F /srv/seafile/XXXX/conf
       |-6600 ccnet-server -F /srv/seafile/XXXX/conf -c /srv/seafile/XXXX/ccnet -f /srv/seafile/XXXX/logs/ccnet.log -d -P /srv/seafile/XXXX/pids/ccnet.pid
       `-6602 seaf-server -F /srv/seafile/XXXX/conf -c /srv/seafile/XXXX/ccnet -d /volume1/Seafile -l /srv/seafile/XXXX/logs/seafile.log -P /srv/seafile/XXXX/pids/seaf-server.pid

Jan 28 17:53:48 XXXX systemd[1]: Starting Seafile...
Jan 28 17:53:48 XXXX start-seafile[6569]: [01/28/18 17:53:48] ../common/session.c(132): using config file /srv/seafile/XXXX/conf/ccnet.conf
Jan 28 17:53:48 XXXX start-seafile[6569]: Starting seafile server, please wait ...
Jan 28 17:53:51 XXXX start-seafile[6569]: Seafile server started
Jan 28 17:53:51 XXXX start-seafile[6569]: Done.
Jan 28 17:53:51 XXXX systemd[1]: Started Seafile.

然而,每次无人值守的更新到来时,都会出现这种情况:

Jan 23 06:38:08 XXXX systemd[1]: Starting Daily apt activities...
Jan 23 06:40:13 XXXX systemd[1]: Reloading.
Jan 23 06:40:13 XXXX systemd[1]: Stopping Seahub...
Jan 23 06:40:14 XXXX systemd[1]: Stopped Seahub.
Jan 23 06:40:14 XXXX systemd[1]: Stopping Seafile...
Jan 23 06:40:15 XXXX systemd[1]: Stopped Seafile.
Jan 23 06:40:15 XXXX systemd[1]: Stopping MySQL Community Server...
Jan 23 06:40:16 XXXX systemd[1]: Stopped MySQL Community Server.
Jan 23 06:40:17 XXXX systemd[1]: Reloading.
Jan 23 06:40:17 XXXX systemd[1]: Stopped MySQL Community Server.
...
Jan 23 06:40:43 XXXX systemd[1]: Reloading.
Jan 23 06:40:44 XXXX systemd[1]: Stopped MySQL Community Server.
Jan 23 06:40:44 XXXX systemd[1]: Reloading.
Jan 23 06:40:44 XXXX systemd[1]: Reloading.
Jan 23 06:40:44 XXXX systemd[1]: Starting MySQL Community Server...
Jan 23 06:40:45 XXXX systemd[1]: Started MySQL Community Server.
Jan 23 06:40:47 XXXX systemd[1]: Reloading.
Jan 23 06:40:48 XXXX systemd[1]: Stopping MySQL Community Server...
Jan 23 06:40:49 XXXX systemd[1]: Stopped MySQL Community Server.
Jan 23 06:40:49 XXXX systemd[1]: Starting MySQL Community Server...
Jan 23 06:40:50 XXXX systemd[1]: Started MySQL Community Server.
(... continues with unrelated services )

所以它意识到 Seafile 需要在 MySQL 之前停止,并且这样做了,但在重新启动后不再启动它 MySQL。

有没有人知道可能导致这种情况的原因? IE。在什么情况下系统服务会在更新期间停止,但不会重新启动?

如果您希望它始终 运行,请使用 Restart=always。当 mysql 服务更新发生时,此服务会执行干净 stop,因此 systemd 不会重新启动它。您设置了 Restart=on-failure,仅当停靠点的 return 代码不是 0.

时才会重新启动
Restart = always
RestartSec = 10

RestartSec
Configures the time to sleep before restarting a service (as configured with Restart=). Takes a unit-less value in seconds, or a time span value such as "5min 20s". Defaults to 100ms.