Skip to content

Instantly share code, notes, and snippets.

@alex-bender
Created December 6, 2017 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-bender/258749007f5e3d7887d81ebc04e3c413 to your computer and use it in GitHub Desktop.
Save alex-bender/258749007f5e3d7887d81ebc04e3c413 to your computer and use it in GitHub Desktop.
wait for pid ends; correct error handling
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