0

Is there a way to PASS a command timeout option to proc = subprocess.Popen(cmd.split(' '), stderr=subprocess.PIPE) to terminate commands that are hung indefinitely

import signal, os
import sys
import subprocess
import argparse
from subprocess import Popen, PIPE, STDOUT
import threading
from time import sleep
import time

from subprocess import Popen, PIPE, STDOUT
cmd = "python script.py -m loc"

proc = subprocess.Popen(cmd.split(' '), stderr=subprocess.PIPE)
print "Executing %s"%cmd
with proc.stderr:
    for line in iter(proc.stderr.readline, b''):
        print line,

print "process is done..." #doesn't print when command is hung

UPDATE:-

I looked at the following tickets,I dont find a clear example on how to use subprocess32,I googled around in python docs,still cant find

Python subprocess timeout?

Using module 'subprocess' with timeout

Community
  • 1
  • 1
  • 1
    Python 3.3+ supports a `timeout` arg. Fort a backport to Python 2, install th the `subprocess32` module. – Corey Goldberg Jul 01 '16 at 00:40
  • Corey - Can I get a sample example on how to use `subprocess32` or how will my current code change using it –  Jul 01 '16 at 00:49
  • Here's a question which addresses timeout: [Using module 'subprocess' with timeout](http://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout) – xgord Jul 01 '16 at 01:06
  • I looked at it already,it is in `In Python 3.3+:`,am looking for a specific example on python 2.x(7) –  Jul 01 '16 at 01:08
  • Sidenote: Why are you doing `for line in iter(proc.stderr.readline, b''):`? File-like objects are iterables of their own lines, you can just do `for line in proc.stderr:` and it will behave the same, run faster, and be more concise to boot. – ShadowRanger Jul 01 '16 at 02:13
  • @ShadowRanger - I looked at the following tickets,I dont find a clear example on how to use `subprocess32`,I googled around in python docs,still cant find `http://stackoverflow.com/questions/3733270/python-subprocess-timeout` `http://stackoverflow.com/questions/1191374/using-module-subprocess-with-timeout` –  Jul 01 '16 at 02:36
  • @user3682248: `subprocess32` is just the Python 3.2 `subprocess` module backported with the timeout feature from 3.3 added. Read [the 3.3 `subprocess` docs](https://docs.python.org/3.3/library/subprocess.html) for usage, you just install and import `subprocess32` instead of importing `subprocess`. – ShadowRanger Jul 01 '16 at 02:41
  • @ShadowRanger - I did that and I get the error `File "logger_output.py", line 14, in proc = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE, stderr=subprocess.PIPE,timeout=30) TypeError: __init__() got an unexpected keyword argument 'timeout'` I added `import subprocess32 as subprocess from subprocess32 import Popen, PIPE, STDOUT` in my code –  Jul 01 '16 at 02:48
  • @user3682248: You didn't read the docs. Seriously. Read the docs I just linked you. Do they say `Popen` takes `timeout` as an argument? No, they do not. READ THE DOCS! – ShadowRanger Jul 01 '16 at 03:02
  • @user3682248: I'll note, in this case, identifying hung commands is hard, because you're blocking on reading from an output pipe, and that blocking action can't be timed (unrelated to subprocess). You need to enforce the timeout yourself, using `select` module primitives to poll the handle for available data & coding your own logic for timeout. Alternatively, you send `stderr` to a real file (e.g. `tempfile.TemporaryFile`) or have a `multiprocessing.Process` do the reading, while the main thread `wait`s on the process with a `timeout` and kills the subprocess if it doesn't exit quickly enough. – ShadowRanger Jul 01 '16 at 03:07

0 Answers0