The following script shows how a funtion is defined that us automatically executed at the before image exit.
#!/usr/bin/env python import sys def exitFunc(): print( " this is exit ") sys.exitfunc = exitFunc print( " hello world ")