I have a script that I want to execute before a machine is either shut down or rebooted but it's only triggered with the start
parameter, never with the stop
parameter and it's driving me crazy.
Following symlinks exist on my system:
/etc/rc0.d/K01init.sh
/etc/rc1.d/K01init.sh
/etc/rc2.d/S99init.sh
/etc/rc3.d/S99init.sh
/etc/rc4.d/S99init.sh
/etc/rc5.d/S99init.sh
/etc/rc6.d/K01init.sh
that all point to the same script /etc/init.d/init.sh. Based on another thread I also tried without the .sh
suffix, which did not change anything.
/etc/init.d/init.sh
#!/bin/sh
### BEGIN INIT INFO
# Provides: init.sh
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: xxx
# Description: xxx
### END INIT INFO
if [ "$1" = "start" ]; then
echo "log message"
//do stuff
fi
echo "$1"
if [ "$1" = "stop" ]; then
echo "log message"
//do stuff
fi
exit 0
The script runs fine during startup, but complains that the cleanup that should have happened in the stop
block was not performed. When I echo the $1
to the logs, it only and always shows start
, which means the OS apparently never sends the stop
command to my script. Running the script from the command line (sudo /etc/init.d/init.sh stop
) works like a charm with both start
and stop
parameters, so I'm confused as to why it does not work when I stop my machine.
Where am I going wrong here? I'm running Ubuntu 18.04 and my current runlevel is 5 (but that should not be relevant, as it should be about 0 & 6 here). Interesting enough, I tried to check for the actual runlevel at execution time by adding echo "$(who -r)"
to the script, which does not output anything to the logs!
If relevant: I'm talking about reboots both via sudo reboot
/ sudo systemctl reboot
and via Azure GUI (it's a virtual machine), not sure if Azures reboot mechanism differs, but neither way works.