def exe(cmd, timeout=-1):
"""
Executes a command through the shell.
timeout in minutes! so 1440 mean is 24 hours.
-1 means never
"""
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, \
stderr=subprocess.STDOUT, close_fds=True,\
preexec_fn=os.setsid)
signal.signal(signal.SIGALRM, alarm_handler)
if timeout > 0:
signal.alarm(int(timeout*60))
try:
stdoutVal, stderrVal = proc.communicate()
signal.alarm(0) # reset the alarm
except Alarm:
logging.error(("Command was taking too long. "
"Automatic Timeout Initiated after %d" % (timeout)))
os.killpg(proc.pid, signal.SIGTERM)
proc.kill()
return 214,None,None
retCode = proc.returncode
return retCode,stdoutVal,stderrVal
评论列表
文章目录