Auto Restarting Python Application

This is a cool sample on how to write an automatic self-restarting python application after a timeout, of course you can modify this application to be much more sophisticated so it can close all open files before restarting or restarting based on a signal (SIGHUP maybe?) or whatever you like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env python

import os, sys, time

def main():
	print "AutoRes is starting"
	executable = sys.executable
	args = sys.argv[:]
	args.insert(0, sys.executable)

	time.sleep(1)
	print "Respawning"
	os.execvp(executable, args)

if __name__ == "__main__":
	main()

Comments