¿Cómo se escribe un servicio systemd que se apaga con gracia al apagar o reiniciar la máquina? En particular, debería retrasar el apagado de la máquina hasta que salga con gracia.
Tengo un servicio que tarda 10 segundos en apagarse: /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"
Configuré TimeoutStopSec en 15s en /etc/systemd/system/shutdowntest.service:
[Service]
ExecStart=/usr/local/bin/shutdowntest.sh
TimeoutStopSec=15
[Install]
WantedBy=multi-user.target
Cuando ejecuto sudo systemctl stop shutdowntest.service, el servicio se cierra con gracia según /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.
Pero cuando yo sudo rebooto sudo shutdown nowla máquina, el servicio se cierra sin tiempo suficiente para salir con gracia, y / var / log / syslog finaliza solo 1 segundo más tarde.
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...
¿Cómo asegurar que el servicio tenga el tiempo ( TimeoutSeco TimeoutStopSec) para salir cuando la máquina se apaga o se reinicia?

journalctl -u shutdowntest.service) en lugar de mirar directamente/var/log/syslog? Me pregunto si syslog no se detiene antes de su servicio y luego no registra el resto de la salidaRespuestas:
El comentario de Bigon es correcto. Estaba buscando
/var/log/syslog, pero esto está escrito porrsyslog.service, que systemd se detiene bastante temprano en el proceso de apagado (como lo indica el "Servicio de registro del sistema detenido" a continuación).Después de activar el registro persistente journald lugar (
Storage=persistenten/etc/systemd/journald.confesystemctl restart systemd-journald),journalctl -b-1 -u shutdowntest.servicemuestra que mi servicio es de hecho se le da suficiente tiempo para apagar después de que el sistema estárebooted,shutdowno después de pulsar la tecla de encendido (ACPI G2 Soft Off).fuente