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)