Quote:
Originally posted by shy
Is there any possibility to catch a signal in bash script?
I would like to do something from dying script (when the system starts to reboot).
|
Yes, that's possible.
What ever you want to do when receiving the TERM signal, make sure you do it quickly.
Code:
#!/bin/bash
on_die()
{
# print message
#
echo "Dying..."
# Need to exit the script explicitly when done.
# Otherwise the script would live on, until system
# realy goes down, and KILL signals are send.
#
exit 0
}
# Execute function on_die() receiving TERM signal
#
trap 'on_die' TERM
# Loop forever, reporting life each second
#
SEC=0
while true ; do
sleep 1
SEC=$((SEC+1))
echo "I'm PID# $$, and I'm alive for $SEC seconds now!"
done
# We never get here.
exit 0