等待服务正常退出,然后关闭机器/重新启动


9

您如何编写在计算机关闭或重新启动后正常关闭的systemd服务?特别是,它应该延迟机器关闭,直到正常退出为止。

我有一个需要10秒钟才能关闭的服务:/usr/local/bin/shutdowntest.sh:

#!/bin/bash

SHUTDOWN=0
SHUTDOWN_TIME=10
TRAPPED_SIGNAL=

function onexit() {
  TRAPPED_SIGNAL=$1
  SHUTDOWN=1
}

for SIGNAL in SIGINT SIGTERM SIGHUP SIGPIPE SIGALRM SIGUSR1 SIGUSR2; do
  trap "onexit $SIGNAL" $SIGNAL
done

echo >&2 "shutdowntest running"
while ((!SHUTDOWN || SHUTDOWN_TIME>0)); do
  if [[ -n "$TRAPPED_SIGNAL" ]]; then
    echo >&2 "shutdowntest received signal $TRAPPED_SIGNAL"
    TRAPPED_SIGNAL=
  elif ((SHUTDOWN)); then
    echo >&2 "shutdowntest Shutting down: $SHUTDOWN_TIME more sleeps"
    SHUTDOWN_TIME=$((SHUTDOWN_TIME-1))
    sleep 1
  else
    sleep 10
  fi
done
echo >&2 "shutdowntest Finished shutting down; quitting"

我在/etc/systemd/system/shutdowntest.service中将TimeoutStopSec设置为15s:

[Service]
ExecStart=/usr/local/bin/shutdowntest.sh
TimeoutStopSec=15

[Install]
WantedBy=multi-user.target

当我运行时sudo systemctl stop shutdowntest.service,服务会根据/var/log/syslog以下内容正常关闭:

00:57:11 shutdowntest.sh[1980]: shutdowntest received signal SIGTERM
00:57:11 shutdowntest.sh[1980]: shutdowntest Shutting down: 10 more sleeps
00:57:11 systemd[1]: Stopping shutdowntest.service...
00:57:11 shutdowntest.sh[1980]: Terminated
00:57:11 shutdowntest.sh[1980]: shutdowntest Shutting down: 9 more sleeps
00:57:12 shutdowntest.sh[1980]: shutdowntest Shutting down: 8 more sleeps
00:57:13 shutdowntest.sh[1980]: shutdowntest Shutting down: 7 more sleeps
00:57:14 shutdowntest.sh[1980]: shutdowntest Shutting down: 6 more sleeps
00:57:15 shutdowntest.sh[1980]: shutdowntest Shutting down: 5 more sleeps
00:57:16 shutdowntest.sh[1980]: shutdowntest Shutting down: 4 more sleeps
00:57:17 shutdowntest.sh[1980]: shutdowntest Shutting down: 3 more sleeps
00:57:18 shutdowntest.sh[1980]: shutdowntest Shutting down: 2 more sleeps
00:57:19 shutdowntest.sh[1980]: shutdowntest Shutting down: 1 more sleeps
00:57:20 shutdowntest.sh[1980]: shutdowntest Finished shutting down; quitting
00:57:20 systemd[1]: Stopped shutdowntest.service.

但是当我sudo rebootsudo shutdown now机器时,服务被杀死,没有足够的时间正常退出,并且/ var / log / syslog仅在1s之后结束。

00:59:30 shutdowntest.sh[2014]: Terminated
00:59:30 shutdowntest.sh[2014]: shutdowntest received signal SIGTERM
00:59:30 shutdowntest.sh[2014]: shutdowntest Shutting down: 10 more sleeps
00:59:30 systemd[1]: Stopping shutdowntest.service...

如何确保在关闭或重新启动计算机后给服务指定的退出时间(TimeoutSec或时间TimeoutStopSec)?


1
systemd应该已经等待服务停止。您可以在日记帐(journalctl -u shutdowntest.service)中进行检查,而不是直接查看/var/log/syslog吗?我想知道syslog是否在您提供服务之前没有停止,然后没有记录其余输出
Bigon

Answers:


4

Bigon评论是正确的。我当时在看/var/log/syslog,但这是由撰写的rsyslog.service,它在关闭过程中很早就停止了systemd的运行(如下面的“停止系统日志记录服务”所示)。

在启用持久日志记录日志后(Storage=persistent/etc/systemd/journald.conf和中systemctl restart systemd-journald),journalctl -b-1 -u shutdowntest.service表明在rebooted shutdown或按下Power键(ACPI G2 Soft Off)后,确实为我的服务提供了足够的时间来关闭。

journalctl -b-1 -u shutdowntest.service -u rsyslog.service -u systemd-logind
-- Logs begin at Mon 2018-03-26 18:39:12 UTC, end at Mon 2018-03-26 20:22:34 UTC. --
…
Mar 26 18:46:46 myhost systemd-logind[1202]: Power key pressed.
Mar 26 18:46:46 myhost systemd-logind[1202]: Powering Off...
Mar 26 18:46:46 myhost systemd-logind[1202]: System is powering down.
Mar 26 18:46:46 myhost shutdowntest.sh[1237]: Terminated
Mar 26 18:46:46 myhost shutdowntest.sh[1237]: shutdowntest received signal SIGTERM
Mar 26 18:46:46 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 10 more sleeps
Mar 26 18:46:46 myhost systemd[1]: Stopping shutdowntest.service...
Mar 26 18:46:46 myhost systemd[1]: Stopping Login Service...
Mar 26 18:46:46 myhost systemd[1]: Stopped Login Service.
Mar 26 18:46:46 myhost systemd[1]: Stopping System Logging Service...
Mar 26 18:46:47 myhost systemd[1]: Stopped System Logging Service.
Mar 26 18:46:47 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 9 more sleeps
Mar 26 18:46:48 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 8 more sleeps
Mar 26 18:46:49 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 7 more sleeps
Mar 26 18:46:50 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 6 more sleeps
Mar 26 18:46:51 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 5 more sleeps
Mar 26 18:46:52 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 4 more sleeps
Mar 26 18:46:53 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 3 more sleeps
Mar 26 18:46:54 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 2 more sleeps
Mar 26 18:46:55 myhost shutdowntest.sh[1237]: shutdowntest Shutting down: 1 more sleeps
Mar 26 18:46:56 myhost shutdowntest.sh[1237]: shutdowntest Finished shutting down; quitting
Mar 26 18:46:56 myhost systemd[1]: Stopped shutdowntest.service.
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.