My guess is that you are running your code in Python 2.
If that is the case, subprocess.check_output()
does not accept a timeout
parameter, and the function will fail immediately with:
TypeError: __init__() got an unexpected keyword argument 'timeout'
But, because you are catching all exceptions and printing a generic message, you don't see the actual exception, and you assume that the command is timing out immediately.
One way to fix this problem is to run your code in Python 3.
Whether you are running Python 2 or 3, I recommend that you do not catch all exceptions, or that you at least print the value of the exception so that you can see the actual cause, e.g.
try:
x=subprocess.check_output(command, shell=True, timeout=5)
except subprocess.TimeoutExpired as exc:
print("Command timed out: {}".format(exc))
exit()
else:
print (x)
which explicitly checks for a timeout exception. All other exceptions are propagated as usual and so are not masked by your "catch all" code. Or,
try:
x=subprocess.check_output(command, shell=True, timeout=5)
except Exception as exc:
print("Command failed: {}".format(exc))
exit()
else:
print (x)
but the former is preferred.
Edit
OP can't use Python 3. If you are using Linux then you could use the timeout
command, e.g.
x = subprocess.check_output('timeout 5 {}'.format(command), shell=True)
On timeout this will raise an exception with a particular exit status value of 124:
subprocess.CalledProcessError: Command 'timeout 5 sleep 10' returned non-zero exit status 124
BTW you shouldn't use the shell=True
option as there are security implications as mentioned in the documentation. Instead you should pass a list of strings to check_output()
like this:
from shlex import shlex
command = shlex('timeout 5 {}'.format(command))
try:
x = subprocess.check_output(command)
except subprocess.CalledProcessError as exc:
if exc.returncode == 124:
print "Command timed out"
else:
raise
If you are using another OS (or you don't want to use timeout
) then you can run your subprocess in a separate thread and have your main thread time it out if required. See this other question, Using module 'subprocess' with timeout, for details about how to do that.