Skip to content

Instantly share code, notes, and snippets.

@zhangchunlin
Last active December 7, 2017 05:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangchunlin/049634ffa265181d74b3b115229717c2 to your computer and use it in GitHub Desktop.
Save zhangchunlin/049634ffa265181d74b3b115229717c2 to your computer and use it in GitHub Desktop.
from subprocess import Popen, PIPE, STDOUT
import os
def runcmd(cwd,cmd):
oldcwd = os.getcwd()
os.chdir(cwd)
runcmd_timeout = 3600
p = Popen(cmd,stdout=PIPE,stderr=STDOUT,shell=True,preexec_fn=os.setpgrp)
p.wait(runcmd_timeout)
if p.returncode==None:
os.kill(-p.pid,9)
out, err = p.communicate()
return p.returncode,out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment