def main(daemonize = True): os.umask(0077) config = parseSettings() web.config._config = config handler = SignalHandler(config['pid_file']) signal.signal(signal.SIGTERM, handler.signal_term_handler) if config['log_file'] == 'stderr': logging.basicConfig(level=config['log_level'], format='* [%(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', stream=sys.stderr) else: log = config['log_file'] if not log.startswith('/'): log = rootdir + '/' + log logging.basicConfig(level=logging.DEBUG, format='* [%(asctime)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename=log) #sys.stderr = Logger() if config['ssl_certificate'] and config['ssl_private_key']: certificate = config['ssl_certificate'] if not certificate.startswith('/'): certificate = rootdir + '/' + certificate key = config['ssl_private_key'] if not key.startswith('/'): key = rootdir + '/' + key CherryPyWSGIServer.ssl_certificate = certificate CherryPyWSGIServer.ssl_private_key = key logging.info('Found certificate and key. Enabling HTTPS') else: logging.info('Certificate and key not found. Disabling HTTPS') if daemonize: try: daemon.daemonize(noClose=False) except Exception, e: logging.error('Error in daemon.daemonize: ' + str(e)) print "Fork failed: %s" % str(e) sys.exit(1) # Run as a low privileged user. uid = pwd.getpwnam(config['user'])[2] # Write pid number into pid file. f = open(config['pid_file'], 'w') f.write(str(os.getpid())) f.close() # Set uid. os.setuid(uid)
def start_daemon(_pid, _cfg): global glue signal.signal(signal.SIGTERM, sigterm_handler) if not os.getenv("DOCKER_MODE") == "True": try: daemon.daemonize(_pid) except RuntimeError as e: print(e, file=sys.stderr) raise SystemExit(1) glue = node.Monitor(_cfg) glue.daemon = True glue.start() while True: if not glue.is_alive(): print('%d: 服务进程丢失,重启进程' % time.time()) glue = node.Monitor(_cfg) glue.daemon = True glue.start() print('%d: new pid=%d' % (time.time(), glue.pid)) sys.stdout.flush() time.sleep(_cfg['global']['monitor_interval'])
def main(): # Set umask. os.umask(0o077) # Establish SQL database connections. db_conns = utils.get_required_db_conns() # Initialize policy daemon. logger.info("Starting iRedAPD (version: {0}, backend: {1}), " "listening on {2}:{3}.".format(__version__, settings.backend, settings.listen_address, settings.listen_port)) local_addr = (settings.listen_address, int(settings.listen_port)) DaemonSocket(local_addr=local_addr, db_conns=db_conns, policy_channel='policy', plugins=settings.plugins) if (settings.srs_secrets and settings.srs_domain): logger.info("Starting SRS sender rewriting channel, listening on " "{0}:{1}".format(settings.listen_address, settings.srs_forward_port)) local_addr = (settings.listen_address, int(settings.srs_forward_port)) DaemonSocket(local_addr=local_addr, db_conns=db_conns, policy_channel='srs_sender') logger.info("Starting SRS recipient rewriting channel, listening on " "{0}:{1}".format(settings.listen_address, settings.srs_reverse_port)) local_addr = (settings.listen_address, int(settings.srs_reverse_port)) DaemonSocket(local_addr=local_addr, db_conns=db_conns, policy_channel='srs_recipient') else: logger.info( "No SRS domain and/or secret strings in settings.py, not loaded.") # Run this program as daemon. if '--foreground' not in sys.argv: try: daemon.daemonize(no_close=True) except Exception as e: logger.error("Error in daemon.daemonize: {0}".format(repr(e))) # Write pid number into pid file. f = open(settings.pid_file, 'w') f.write(str(os.getpid())) f.close() # Get uid/gid of daemon user. p = pwd.getpwnam(settings.run_as_user) uid = p.pw_uid gid = p.pw_gid # Run as daemon user os.setgid(gid) os.setuid(uid) # Create a global dict used to track smtp session data. # - gather data at RCPT state # - used in END-OF-MESSAGE state # - clean up after applied all enabled plugins settings.GLOBAL_SESSION_TRACKING = {} # Starting loop. try: if sys.version_info >= (3, 4): # workaround for the "Bad file descriptor" issue on Python 2.7, gh-161 asyncore.loop(use_poll=True) else: # fixes the "Unexpected communication problem" issue on Python 2.6 and 3.0 asyncore.loop(use_poll=False) except KeyboardInterrupt: pass except Exception as e: logger.error("Error in asyncore.loop: {0}".format(repr(e)))