I need to write a process wrapper in python that will launch an application and restart it if it fails. This will be logged to an specified log location passed in from command line. Is this possible?
Sean Vieira
161k34 gold badges321 silver badges297 bronze badges
asked Sep 1, 2011 at 16:58
Renjith
5221 gold badge8 silver badges29 bronze badges
-
Describe fail. Some error on stderr? return code?utdemir– utdemir2011年09月01日 17:03:24 +00:00Commented Sep 1, 2011 at 17:03
-
Advice: Search. stackoverflow.com/… The following may answer your question. stackoverflow.com/questions/3850708/python-multiprocessing. If it's not appropriate, please provide some details so we know what you're talking about.S.Lott– S.Lott2011年09月01日 17:06:06 +00:00Commented Sep 1, 2011 at 17:06
2 Answers 2
>>> from subprocess import Popen
>>> def spawner(cmd_list):
... while True:
... print "Running proc..."
... mon_proc = Popen(cmd_list)
... print "Proc exit: %s" % mon_proc.wait()
...
>>> spawner(['/bin/sleep', '3'])
Running proc...
Proc exit: 0
Running proc...
Proc exit: 0
Running proc...
Proc exit: 0
Running proc...
answered Sep 1, 2011 at 17:41
tMC
19.5k15 gold badges68 silver badges102 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Use the subprocess module. Use Popen to start it and get a Popen objet. Use Popen.poll() or wait to get process status depending of what you want. Do it in a loop, and log using the logging module.
my 2 cents
answered Sep 1, 2011 at 17:08
neuro
15.2k3 gold badges38 silver badges61 bronze badges
Comments
lang-py