Wie schreibt man einen systemd-Dienst, der beim Herunterfahren oder Neustart des Computers ordnungsgemäß heruntergefahren wird? Insbesondere sollte es das Herunterfahren der Maschine verzögern, bis es ordnungsgemäß beendet wird.
Ich habe einen Dienst, dessen Herunterfahren 10 Sekunden dauert: /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"
Ich habe TimeoutStopSec in /etc/systemd/system/shutdowntest.service auf 15s gesetzt:
[Service]
ExecStart=/usr/local/bin/shutdowntest.sh
TimeoutStopSec=15
[Install]
WantedBy=multi-user.target
Wenn ich laufe sudo systemctl stop shutdowntest.service, runter der Shuts graziös nach /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.
Aber wenn ich sudo rebootoder sudo shutdown nowder Computer, wird der Dienst ohne genügend Zeit beendet, um ordnungsgemäß beendet zu werden, und / var / log / syslog endet nur 1s später.
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...
Wie kann sichergestellt werden, dass der Dienst die Zeit ( TimeoutSecoder TimeoutStopSec) zum Beenden erhält , wenn der Computer heruntergefahren oder neu gestartet wird?
journalctl -u shutdowntest.service) nachsehen, anstatt direkt hineinzuschauen/var/log/syslog? Ich frage mich, ob Syslog nicht vor Ihrem Dienst gestoppt wird und dann den Rest der Ausgabe nicht protokolliert