Sometimes you want to clean up some things when your script gets stopped for whatever reason.
This script captures the SIGINT signal and sends it to the myExit function.
The myExit function calls another function (just to show what is possible) which removes a logfile.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#!/bin/bash myCleanup() { rm -f /myapp/tmp/mylog return $? } myExit() { echo -en "n*** Exiting ***n" myCleanup exit $? } trap myExit SIGINT # main loop while true do echo -n "Enter your name: " read x echo "Hello $x" done |
If you map the SIGINT signal to a function which would do nothing, keep in mind, you might get stuck 😉