I have a script that I want to execute before a machine is either shut down or rebooted:
/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"
su - azureuser -c '/bin/bash /my/script/dir/script1.sh'
echo "log message"
su - azureuser -c '/bin/bash /my/script/dir/script2.sh -p "parameter2"'
fi
if [ "$1" = "stop" ]; then
echo "log message"
su - azureuser -c '<some shell command>'
cd /my/script/dir/sub
./script3.sh parameter3
su - azureuser -c 'cd /my/script/dir/sub; ./script4.sh parameter4 --option1 --option2 --option3 somevalue'
fi
exit 0
(I obfuscated the paths and filenames but retained the overall structure for this thread, obviously nothing is named like it is here)
The script runs fine during startup, but complains that the cleanup that should have happened in the stop
block was not performed and I cannot see any log message that's originated in the stop
block in the syslog. 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, pretty sure the runlevel scripts run as root.
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.
Permissions for script1
and script2
are both 750
and for script3
and script4
even 755.
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). The script itself (including sub-scripts) is not at discussion here, it works fine in another environment and manual runs lead to the desired state in this environment, so my setup (or understanding) of init scripts has to be wrong somewhere.
If relevant: I'm talking about reboots both via sudo reboot
and via Azure GUI (it's a virtual machine), not sure if Azures reboot mechanism differs. Neither way works.