Created
December 2, 2008 17:11
-
-
Save atifaziz/31182 to your computer and use it in GitHub Desktop.
Re-launches a process if it dies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Process Monitor | |
# | |
# Re-launches a process if it dies. | |
# | |
# Public Domain | |
# http://en.wikipedia.org/wiki/Public_domain | |
# Written by | |
# Atif Aziz, http://www.raboof.com | |
# Adapted from | |
# http://bartdesmet.net/blogs/bart/archive/2006/08/30/4366.aspx | |
# | |
# NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. | |
import sys | |
from System import Console | |
from System.Diagnostics import Process, ProcessStartInfo | |
def launch(binpath, args): | |
psi = ProcessStartInfo(FileName = binpath, Arguments = args) | |
proc = Process(StartInfo = psi, UseShellExecute = False, EnableRaisingEvents = True) | |
def onexit(sender, event_args): | |
print 'Process %d stopped. Re-starting...' % proc.Id | |
launch(binpath, args) | |
proc.Exited += onexit | |
proc.Start() | |
print 'Process %d started' % proc.Id | |
def main(args): | |
if not args: | |
raise Exception('Missing binpath to executable.') | |
launch(args[0], ' '.join(['"%s"' % arg for arg in args[1:]])) | |
Console.ReadLine() | |
if __name__ == '__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment