Skip to content
Browse files

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 <jlaska@redhat.com>
Signed-off-by: Lucas Meneghel Rodrigues <lmr@redhat.com>


git-svn-id: svn://test.kernel.org/autotest/trunk@5380 592f7852-d20e-0410-864c-8624ca9c26a4
  • Loading branch information...
1 parent e30230f commit 2b095237a634a9865e324b5b46c15b089e64ba67 lmr committed May 25, 2011
Showing with 46 additions and 14 deletions.
  1. +6 −1 scheduler/babysitter_logging_config.py
  2. +40 −13 scheduler/monitor_db_babysitter
View
7 scheduler/babysitter_logging_config.py
@@ -3,8 +3,13 @@
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,
View
53 scheduler/monitor_db_babysitter
@@ -12,23 +12,20 @@ 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__), '..'))
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()

0 comments on commit 2b09523

Please sign in to comment.
Something went wrong with that request. Please try again.