Skip to content

Instantly share code, notes, and snippets.

@bibiboot
Created October 31, 2012 05:51
Show Gist options
  • Save bibiboot/3985125 to your computer and use it in GitHub Desktop.
Save bibiboot/3985125 to your computer and use it in GitHub Desktop.
Process timeout and status check
def restart(timeout=5):
"""
Script polls the process checking
for the status of the process.
"""
status, success = "Restart successfull....", True
start = datetime.now()
proc = subprocess.Popen(CMD, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Polling to have process timeout
while proc.poll() is None:
time.sleep(0.1)
now = datetime.now()
if (now - start).seconds > timeout:
os.kill(proc.pid, signal.SIGKILL)
os.waitpid(-1, os.WNOHANG)
return "Server restart timeout", False
if vars(proc)['returncode'] == 1:
success = False
return status, success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment