Skip to content

Instantly share code, notes, and snippets.

@sciurus
Last active December 24, 2015 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sciurus/6720021 to your computer and use it in GitHub Desktop.
Save sciurus/6720021 to your computer and use it in GitHub Desktop.
Process monitor
#!/usr/bin/env python
import os, re, signal, subprocess
def monitor():
print "Starting procedure"
p = subprocess.Popen('./problematic_procedure.py',
stdout=subprocess.PIPE
)
while p.poll() is None:
line = p.stdout.readline()
print "Procedure output was %s" % line
if re.match('bad', line):
os.kill(p.pid, signal.SIGTERM)
print "Procedure failed so I killed it"
monitor()
monitor()
print 'Procedure exited'
#!/usr/bin/env python
import random, sys, time
while True:
r = random.randint(1,6)
if r == 1:
sys.exit()
elif r == 2 or r == 3:
print "bad"
sys.stdout.flush()
else:
print "good"
sys.stdout.flush()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment