def create_process(f): self.process = subprocess.Popen( cmd, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr, universal_newlines=True, preexec_fn=lambda: os.setpgrp(), shell=False, ) daemon.write_pid(f, self.process.pid)
def main(): try: init_options() except OptionsError as e: sys.stderr.write("Error: {0}\n\n".format(e)) sys.stderr.write(usage()) sys.stderr.write("\n") sys.stderr.flush() sys.exit(os.EX_CONFIG) if options.main.generate_config: print(generate_sample_config() ) # XXX: this doesn't yet work properly because of groper sys.exit() conf_root = os.path.dirname(os.path.abspath(options.main.config)) facility_db = FacilityDB() try: facility_db.load_config( normalize_path(options.main.facilities_config, conf_root)) except (IOError) as e: sys.stderr.write("Error reading {0}: {1}\n".format( options.main.facilities_config, e)) sys.stderr.flush() sys.exit(os.EX_CONFIG) except (FacilityError, configparser.Error) as e: sys.stderr.write("{0} contains errors:\n\n".format( options.main.facilities_config)) if hasattr(e, 'lineno'): e = 'Error on line {0}: {1}'.format( e.lineno, e.message.split("\n")[0].strip()) sys.stderr.write("{0}\n\n".format(e)) sys.stderr.write("Exiting now.\n") sys.stderr.flush() sys.exit(os.EX_CONFIG) if options.main.check_config: sys.exit() # We are just checking the config file, so exit here. cache_config_checksum() create_dirs() if options.main.daemon: daemonize() if options.main.user: drop_privileges(options.main.user) if options.main.pidfile: write_pid(options.main.pidfile) atexit.register(exit_handler) setup_logging() try: logging.getLogger().info("Starting loghogd.") compressor = Compressor() compressor.find_uncompressed(options.main.logdir, r'.+\.log\..+') writer = Writer(facility_db, compressor, options.main.logdir) processor = Processor(facility_db, writer) server = Server(processor.on_message, conf_root) signal_handler = make_shutdown_handler(server, writer, compressor) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGHUP, make_reload_handler(facility_db, writer)) except Exception as e: logging.getLogger().error(e) logging.getLogger().error( 'Exiting abnormally due to an error at startup.') sys.exit(os.EX_CONFIG) try: compressor.start() server.run() except Exception as e: logging.getLogger().exception(e) logging.getLogger().error( 'Exiting abnormally due to an error at runtime.') shutdown(None, server, writer, compressor) sys.exit(os.EX_SOFTWARE) logging.getLogger().info('Shutdown complete. Exiting.')
def main(): try: init_options() except OptionsError as e: sys.stderr.write("Error: {0}\n\n".format(e)) sys.stderr.write(usage()) sys.stderr.write("\n"); sys.stderr.flush() sys.exit(os.EX_CONFIG) if options.main.generate_config: print(generate_sample_config()) # XXX: this doesn't yet work properly because of groper sys.exit() conf_root = os.path.dirname(os.path.abspath(options.main.config)) facility_db = FacilityDB() try: facility_db.load_config(normalize_path(options.main.facilities_config, conf_root)) except (IOError) as e: sys.stderr.write("Error reading {0}: {1}\n".format(options.main.facilities_config, e)) sys.stderr.flush() sys.exit(os.EX_CONFIG) except (FacilityError, configparser.Error) as e: sys.stderr.write("{0} contains errors:\n\n".format(options.main.facilities_config)) if hasattr(e, 'lineno'): e = 'Error on line {0}: {1}'.format(e.lineno, e.message.split("\n")[0].strip()) sys.stderr.write("{0}\n\n".format(e)) sys.stderr.write("Exiting now.\n") sys.stderr.flush() sys.exit(os.EX_CONFIG) if options.main.check_config: sys.exit() # We are just checking the config file, so exit here. cache_config_checksum() create_dirs() if options.main.daemon: daemonize() if options.main.user: drop_privileges(options.main.user) if options.main.pidfile: write_pid(options.main.pidfile) atexit.register(exit_handler) setup_logging() try: logging.getLogger().info("Starting loghogd.") compressor = Compressor() compressor.find_uncompressed(options.main.logdir, r'.+\.log\..+') writer = Writer(facility_db, compressor, options.main.logdir) processor = Processor(facility_db, writer) server = Server(processor.on_message, conf_root) signal_handler = make_shutdown_handler(server, writer, compressor) signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGHUP, make_reload_handler(facility_db, writer)) except Exception as e: logging.getLogger().error(e) logging.getLogger().error('Exiting abnormally due to an error at startup.') sys.exit(os.EX_CONFIG) try: compressor.start() server.run() except Exception as e: logging.getLogger().exception(e) logging.getLogger().error('Exiting abnormally due to an error at runtime.') shutdown(None, server, writer, compressor) sys.exit(os.EX_SOFTWARE) logging.getLogger().info('Shutdown complete. Exiting.')
def create_process(f): self.process = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=self.stdout, stderr=self.stderr) daemon.write_pid(f, self.process.pid)