This is a sleeping service,
import time bufsize = 0 f = open("/home/user/sleep-log", "w", bufsize) try: f.write("going for a deep sleep......") time.sleep( 1000000000) except: import traceback; f.write(traceback.format_exc()) finally: f.write("that is surprising.. final block just ran")
And i have a ubuntu upstart task to run this,
description "sleeper" author "saravana" start on runlevel  stop on runlevel [!2345] script exec /usr/bin/python /home/user/a.py end script
When i do,
service sleeper start service sleeper stop
the log file contains,
going for a deep sleep.......
Finally block is skipped. How can i ensure my cleanup code will always run? I can do it in conf file using
post stop script but for legacy reasons, i want to do it inside python file.