def TIMEOUT_COMMAND(command, timeout):
    """call shell-command and either return its output or kill it
    if it doesn't normally exit within timeout seconds and return None"""
    import subprocess, datetime, os, time, signal,sys
    cmd = command.split(" ")
    start = datetime.datetime.now()
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)
    while process.poll() is None:
        time.sleep(1)
        now = datetime.datetime.now()
        if (now - start).seconds> timeout:
            
            subprocess.Popen("cmd /c taskkill /f /im notepad.exe")
            
            return False
    return True


print TIMEOUT_COMMAND("notepad.exe",5)

  

posted on 2016-06-03 11:25  小白闯天下  阅读(3407)  评论(0编辑  收藏  举报