In Python, I have a program snippet similar to the following that has the effect of running a custom command and returning the stdout data (or raise exception when exit code is non-zero):
proc = subprocess.Popen(
cmd,
# keep stderr separate; or merge it with stdout (default).
stderr=(subprocess.PIPE if ignore_stderr else subprocess.STDOUT),
stdout=subprocess.PIPE,
shell=True)
And then I use communicate
(not wait
which could deadlock) to wait for the complete stdout data:
stdoutdata, stderrdata = proc.communicate()
My question is - how do I set a timeout for any command? For example, I don't want the program to wait indefinitely because a particular programs takes more than, say, 5 minutes to run.
Simpler, unsophisticated solutions would be nice.