This is largely based on Non-blocking read on a subprocess.PIPE in python
We use a separate thread and Queue to check for output after a certain amount of time, given in the q.get(timeout=5)
portion. Change it to 60
if you only want to kill the process after 60 seconds. Change the first argument to subprocess.Popen
to an appropriate command you want to run.
import Queue
import subprocess
import threading
def checkStdout(p, q):
s = p.stdout.read(1)
if len(s) > 0:
q.put(s)
def run():
p = subprocess.Popen(['cat'], stdout=subprocess.PIPE, close_fds=True)
print 'pid %d started' % p.pid
q = Queue.Queue()
t = threading.Thread(target=checkStdout, args=(p, q))
t.daemon = True
t.start()
try:
canGet = q.get(timeout=5)
print 'has output'
except Queue.Empty:
print 'no output'
# kill process
p.kill()
if __name__ == '__main__':
run()