Created
December 6, 2017 08:34
-
-
Save alex-bender/258749007f5e3d7887d81ebc04e3c413 to your computer and use it in GitHub Desktop.
wait for pid ends; correct error handling
This file contains 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
import os | |
import time | |
import sys | |
from subprocess import call | |
def check_pid(pid): | |
""" Check For the existence of a unix pid. """ | |
while True: | |
try: | |
os.kill(pid, 0) | |
except OSError: | |
# pid is not running | |
print('Proc done') | |
call('/usr/sbin/pm-hibernate') | |
os._exit(0) | |
else: | |
print('wait for it') | |
time.sleep(5) | |
if __name__ == '__main__': | |
check_pid(int(sys.argv[1])) | |
# =============================================================== | |
import os | |
def pid_exists(pid): | |
if pid < 0: return False #NOTE: pid == 0 returns True | |
try: | |
os.kill(pid, 0) | |
except ProcessLookupError: # errno.ESRCH | |
return False # No such process | |
except PermissionError: # errno.EPERM | |
return True # Operation not permitted (i.e., process exists) | |
else: | |
return True # no error, we can send a signal to the process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment