Skip to content

Instantly share code, notes, and snippets.

@zhangchunlin
Last active December 7, 2017 05:58
Show Gist options
  • Select an option

  • Save zhangchunlin/049634ffa265181d74b3b115229717c2 to your computer and use it in GitHub Desktop.

Select an option

Save zhangchunlin/049634ffa265181d74b3b115229717c2 to your computer and use it in GitHub Desktop.

Revisions

  1. zhangchunlin revised this gist Dec 7, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions run_subprocess.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    from subprocess import Popen, PIPE, STDOUT
    import os

    def runcmd(cwd,cmd):
    oldcwd = os.getcwd()
  2. zhangchunlin created this gist Dec 6, 2017.
    12 changes: 12 additions & 0 deletions run_subprocess.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    from subprocess import Popen, PIPE, STDOUT

    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