Reputation: 1484
I am using python command
import commands
tmp = "my command" #any command like ls
status, output = commands.getstatusoutput(tmp)
It works perfectly. Now i have few commands which may take more than 5 seconds or stuck forever. I want to kill such commands after 5 second. Any pointers.
Upvotes: 1
Views: 262
Reputation: 1484
I figured out the easiest solution:
from easyprocess import EasyProcess
tmp = "my command" #any command like ls
s = EasyProcess(tmp).call(timeout=5)
print "return_code: ",s.return_code
print "output: ", s.stdout
Upvotes: 0
Reputation: 137
You can use the subprocess module (commands
is deprecated).
import shlex, subprocess, time
command = "command"
args = shlex.split(command)
p = subprocess.Popen(args)
start_time = time.time()
while time.time() - start_time < timeout:
time.sleep(0.1)
if p.poll() is not None:
break
if p.returncode is not None:
p.kill()
Upvotes: 1