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.
# print message
# Need to exit the script explicitly when done.
# Otherwise the script would live on, until system
# realy goes down, and KILL signals are send.
# Execute function on_die() receiving TERM signal
trap 'on_die' TERM
# Loop forever, reporting life each second
while true ; do
echo "I'm PID# $$, and I'm alive for $SEC seconds now!"
# We never get here.