Tuesday, September 7, 2010

Altering the behavior of CTRL-C

Short piece of code that might come in handy. One can use the "trap" procedure to alter the behavior of Ctrl-C which is often used to interrupt execution. One can use the altered behavior to delete back-ground processes or remove temporary files that might otherwise keep lying around.. So here is the src code which you can add anywhere in your script.

# trap ctrl-c and call ctrl_c()
trap 'ctrl_c' 2

function ctrl_c() {

pkill temp_processes
#run clean up code here ...