How do you write a systemd service that gracefully shuts down upon machine shutdown or reboot? In particular, it should delay machine shutdown until it exits gracefully.
I have a service that takes 10 seconds to shut down: /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"
I set TimeoutStopSec to 15s in /etc/systemd/system/shutdowntest.service:
[Service]
ExecStart=/usr/local/bin/shutdowntest.sh
TimeoutStopSec=15
[Install]
WantedBy=multi-user.target
When I run sudo systemctl stop shutdowntest.service
, the service shuts down gracefully according to /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.
But when I sudo reboot
or sudo shutdown now
the machine, the service is killed without enough time to exit gracefully, and the /var/log/syslog ends only 1s later.
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...
How to ensure that the service is given the time (TimeoutSec
or TimeoutStopSec
) to exit when the machine is shutdown or rebooted?
The comment from Bigon is correct. I was looking in
/var/log/syslog
, but this is written byrsyslog.service
, which systemd stops pretty early in the shutdown process (as indicated by “Stopped System Logging Service” below).After I enabled persistent journald logging instead (
Storage=persistent
in/etc/systemd/journald.conf
andsystemctl restart systemd-journald
),journalctl -b-1 -u shutdowntest.service
shows that my service is indeed given enough time to shut down after the system isreboot
ed,shutdown
, or after the Power key is pressed (ACPI G2 Soft Off).