def main(): parser = OptionParser() parser.add_option("-r", action="store_true", dest="recover", help=("run recovery mode (implicit after any crash)")) parser.add_option("--background", dest="background", action="store_true", default=False, help=("runs the scheduler monitor on " "background")) (options, args) = parser.parse_args() recover = (options.recover == True) if len(args) != 0: parser.print_help() sys.exit(1) if os.getuid() == 0: logging.critical("Running as root, aborting!") sys.exit(1) if utils.program_is_alive(monitor_db.WATCHER_PID_FILE_PREFIX): logging.critical("autotest-monitor-watcher already running, aborting!") sys.exit(1) utils.write_pid(monitor_db.WATCHER_PID_FILE_PREFIX) if options.background: logging_manager.configure_logging( watcher_logging_config.WatcherLoggingConfig(use_console=False)) # Double fork - see http://code.activestate.com/recipes/66012/ try: pid = os.fork() if (pid > 0): sys.exit(0) # exit from first parent except OSError, e: sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment os.chdir("/") os.umask(0) os.setsid() # Second fork try: pid = os.fork() if (pid > 0): sys.exit(0) # exit from second parent except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1)
sys.stderr.write("fork #1 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) # Decouple from parent environment os.chdir("/") os.umask(0) os.setsid() # Second fork try: pid = os.fork() if (pid > 0): sys.exit(0) # exit from second parent except OSError, e: sys.stderr.write("fork #2 failed: (%d) %s\n" % (e.errno, e.strerror)) sys.exit(1) else: logging_manager.configure_logging( watcher_logging_config.WatcherLoggingConfig()) while True: proc = MonitorProc(do_recovery=recover) proc.start() time.sleep(PAUSE_LENGTH) while proc.is_running(): logging.info("Tick") time.sleep(PAUSE_LENGTH) recover = False