def do_monitor(args):
""" Handle "monitor" mode.
"""
# If we aren't running in monitor mode, then we are done.
if not args.monitor:
return
# This is the main retry loop.
while True:
# Fork the process.
logger.info('Forking child process.')
pid = os.fork()
# If we are the child, leave this function and work.
if pid == 0:
logger.debug('We are a newly spawned child process.')
return
logger.debug('Child process spawned: %d', pid)
# Wait for the child to die. If we die first, kill the child.
atexit.register(kill_process, pid)
try:
_, exit_status = os.waitpid(pid, 0)
except KeyboardInterrupt:
break
atexit.unregister(kill_process)
# Process the exit code.
signal_number = exit_status & 0xFF
exit_code = (exit_status >> 8) & 0xFF
core_dump = bool(0x80 & signal_number)
if signal_number == 0:
logger.info('Child process exited with exit code: %d.', exit_code)
else:
logger.info('Child process exited with signal %d (core dump: %s).',
signal_number, core_dump)
retry = False
if os.WIFSIGNALED(exit_status):
if os.WTERMSIG(exit_status) == signal.SIGSEGV:
logger.error('Child process seg faulted.')
retry = True
if not retry:
break
sys.exit(0)
###############################################################################
评论列表
文章目录