I have a script which loops through an array and performs an action for each itetation which takes several seconds. Also, at the beginning of the script, a driver is loaded which has to be properly unloaded in order to not crash the hardware. Problem is that if you accidentally create a large array, you are stuck with two options: Either you Ctrl+C and go do the old unplug-replug, or you wait for 40h. Both are not feasable. Is there a way, then, to call a function upon the user terminating the program?