def kill_and_wait(pid, pidfile):
# The NGAS server should nicely shut down itself after receiving this signal
os.kill(pid, signal.SIGTERM)
# We previously checked that the pidfile was gone, but this is not enough
# In some cases the main thread finished correctly, but there are other
# threads (e.g., HTTP servicing threads) that are still running,
# and thus the PID file disappears but the process is still ongoing
tries = 0
max_tries = 20
sleep_period = 1
start = time.time()
while tries < max_tries:
# SIGCONT can be sent many times without fear of side effects
# If we get ESRCH then the process has finished
try:
os.kill(pid, signal.SIGCONT)
except OSError as e:
if e.errno == errno.ESRCH:
# Bingo! the process is gone
break
tries += 1
time.sleep(sleep_period)
sys.stdout.write('.')
sys.stdout.flush()
sys.stdout.write('\n')
sys.stdout.flush()
end = time.time()
ret = 0x00
# We waited a lot but the process is still there, kill it with 9
if tries == max_tries:
err("Process didn't die after %.2f [s], killing it with -9" % (end-start))
os.kill(pid, signal.SIGKILL)
ret += 0x01
# The process should have removed its own pidfile...
if os.path.exists(pidfile):
err("Removing PID file manually, the daemon process didn't remove it by itself")
os.unlink(pidfile)
ret += 0x02
return ret
评论列表
文章目录