From 2b095237a634a9865e324b5b46c15b089e64ba67 Mon Sep 17 00:00:00 2001 From: lmr Date: Wed, 25 May 2011 20:12:12 +0000 Subject: [PATCH] scheduler monitor (monitor_db_babysitter): Add --background option The init script libraries for Red Hat based systems don't contain the start-stop-daemon function present on /lib/lsb/init-functions, commonly present on debian/ubuntu boxes. So, in order to implement a init script for RH-based systems, introduce the --background option for the scheduler monitor, which will fork it and detach it from the parent terminal. In order to do that, defer the logging configuration a little, also modify the interfaces for the logging config functions of this module a little. Signed-off-by: James Laska Signed-off-by: Lucas Meneghel Rodrigues git-svn-id: svn://test.kernel.org/autotest/trunk@5380 592f7852-d20e-0410-864c-8624ca9c26a4 --- scheduler/babysitter_logging_config.py | 7 ++++- scheduler/monitor_db_babysitter | 53 +++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/scheduler/babysitter_logging_config.py b/scheduler/babysitter_logging_config.py index cf2d699c..dde41a85 100644 --- a/scheduler/babysitter_logging_config.py +++ b/scheduler/babysitter_logging_config.py @@ -3,8 +3,13 @@ import logging from autotest_lib.client.common_lib import logging_config class BabysitterLoggingConfig(logging_config.LoggingConfig): + def __init__(self, use_console=True): + self.use_console = use_console + super(BabysitterLoggingConfig, self).__init__() + def configure_logging(self): - super(BabysitterLoggingConfig, self).configure_logging(use_console=True) + super(BabysitterLoggingConfig, self).configure_logging( + use_console=self.use_console) self.add_file_handler(self.get_timestamped_log_name('babysitter'), logging.DEBUG, diff --git a/scheduler/monitor_db_babysitter b/scheduler/monitor_db_babysitter index aac6c4e8..4625781d 100755 --- a/scheduler/monitor_db_babysitter +++ b/scheduler/monitor_db_babysitter @@ -12,7 +12,11 @@ PAUSE_LENGTH = 60 STALL_TIMEOUT = 2*60*60 parser = OptionParser() -parser.add_option("-r", action="store_true", dest="recover") +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() autodir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) @@ -20,15 +24,8 @@ results_dir = os.path.join(autodir, 'results') monitor_db_path = os.path.join(autodir, 'scheduler/monitor_db.py') recover = (options.recover == True) -# load logging settings -logging_manager.configure_logging( - babysitter_logging_config.BabysitterLoggingConfig()) - if len(args) != 0: - print "Usage: %s [options]" % __file__ - print " -r Run recovery mode. (Note: recovery is implicit after" - print " any crash!)" - print + parser.print_help() sys.exit(1) @@ -142,17 +139,47 @@ class MonitorProc(SiteMonitorProc): log.close() -logging.info("initializing") - if os.getuid() == 0: - logging.critical("running as root, aborting!") + logging.critical("Running as root, aborting!") sys.exit(1) if utils.program_is_alive(monitor_db.BABYSITTER_PID_FILE_PREFIX): - logging.critical("monitor_db_babysitter already running, aborting!") + logging.critical("Monitor_db_babysitter already running, aborting!") sys.exit(1) + utils.write_pid(monitor_db.BABYSITTER_PID_FILE_PREFIX) +if options.background: + logging_manager.configure_logging( + babysitter_logging_config.BabysitterLoggingConfig(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) +else: + logging_manager.configure_logging( + babysitter_logging_config.BabysitterLoggingConfig()) + + while True: proc = MonitorProc(do_recovery=recover) proc.start() -- 2.11.4.GIT