Created
October 31, 2012 05:51
-
-
Save danish-rehman/3985125 to your computer and use it in GitHub Desktop.
Process timeout and status check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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