A bash function to catch program exit signals.
#!/usr/bin/env bash
##
# Trap Control C (SIGTERM)
# https://raymii.org/s/snippets/Bash_Bits_Trap_Control_C_SIGTERM.html
##
#
# When the script gets killed, or you do a ctrl + c, the script will remove it's
# pid file. You can put anything in the "control_c" function below, I mostly use
# it for cleanup.
function control_c {
echo -en "\n## Caught SIGINT; Clean up and Exit \n"
rm /var/run/myscript.pid
exit $?
}
#
# Usage
#
trap control_c SIGINT
trap control_c SIGTERM