The script stop_cluster.sh should get executed during system bootup/shutdown. Email notification should be sent to confirm the execution of the script.
If the script is placed in /etc/rc.d/ directory (For example /etc/rc.d/rc3.d/sample-testmail) it would not get executed because it is not a service started by SysV. Even if the script is moved to rc0.d it would be executed during the shutdown. To make SysV run a script, make it the SysV way, and for that merge the start script and stop script into a single script (which is a service) and have start/stop functions in the script.
1. Write a combined script for for system shutdown and startup in /etc/init.d/ ( For example /etc/init.d/sample-testmail ). Or merge the start and stop script into one single script (a service), and have the start() and stop() functions in the script. An example script placed in /etc/init.d si shown below:
# cat /etc/init.d/sample-testmail #!/bin/bash # # sample-testmail Test script for sending a mail during startup and stop # # chkconfig: 2345 90 20 # description: Toy script for testing ### BEGIN INIT INFO # Provides: Toy script for sending mail # Required-Start: $local_fs $network $remote_fs # Required-Stop: $local_fs $network $remote_fs # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Send a mail during system start up and shutdown # Description: Toy script for testing. ### END INIT INFO case "$1" in start) touch /var/lock/subsys/sample-testmail echo "start" `date +%s` >> /tmp/shutdowntest.out mailx -s "start `date +%s`" root < /tmp/testfile ;; stop) rm /var/lock/subsys/sample-testmail echo "stop" `date +%s` >> /tmp/shutdowntest.out mailx -s "stop `date +%s`" root < /tmp/testfile ;; *) echo "other" `date +%s` >> /tmp/shutdowntest.out ;; esac exit 0
2. Add it to chkconfig so that it will start/stop during system bootup/shutdown.
# chkconfig --add sample-testmail # chkconfig --list | grep sample sample-testmail 0:off 1:off 2:on 3:on 4:on 5:on 6:off
3. Reboot the system and check the mails to verify it.
# shutdown -r now
# mail Heirloom Mail version 12.4 7/29/08. Type ? for help. "/var/spool/mail/root": 2 messages 2 new N 1 root Tue Apr 3 20:07 18/633 "stop 1522757262" N 2 root Tue Apr 3 20:08 18/634 "start 152275730