def main(): parser = get_parser() opts, args = parser.parse_args() load_config_or_exit(opts.configfile) if opts.check and opts.background: parser.error( '--check --background makes no sense, how will you know the result?' ) if not opts.background: log_to_stream(sys.stderr, level=logging.DEBUG if opts.debug else logging.WARNING) return doit(opts) else: pidlockfile = PIDLockFile(PIDFILE) existing_pid = pidlockfile.read_pid() if existing_pid: if process_is_alive(existing_pid): sys.stderr.write( 'Another beaker-init process is running (pid %s)\n' % existing_pid) return 1 else: sys.stderr.write('Pid file %s exists but pid %s is dead, ' 'removing the pid file\n' % (PIDFILE, existing_pid)) pidlockfile.break_lock() with daemon.DaemonContext(pidfile=pidlockfile, detach_process=True): log_to_syslog('beaker-init') return doit(opts)
def start(daemon=True, **kwargs): lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid")) if lock.is_locked(): try: Process(lock.read_pid()) print("Rammon is already running") sys.exit(1) except NoSuchProcess: print( "Rammon was stopped, however it was stopped uncleanly leaving behind a pidfile.\n" "Breaking pidfile lock...") lock.break_lock() if daemon: logger.info("Starting rammon as a daemon...") else: logger.info("Starting rammon in the foreground...") if daemon: if try_systemd_start(): logger.info("Starting with systemd...") sys.exit(0) else: mon = RamMonitor() set_handlers(mon) logger.info("Starting without systemd...") with DaemonContext(umask=0o077, pidfile=lock, detach_process=daemon): mon.start() else: with lock: mon = RamMonitor() set_handlers(mon) mon.start()
def status(**kwargs): lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid")) if lock.is_locked(): try: Process(lock.read_pid()) print("Rammon is running" + (", and auto-start is enabled" if is_enabled() else "")) except NoSuchProcess: print( "Rammon is stopped, however it was stopped uncleanly leaving behind a pidfile.\n" "Breaking pidfile lock...") lock.break_lock() else: print("Rammon is stopped" + (", but auto-start is enabled" if is_enabled() else ""))
def stop(**kwargs): lock = PIDLockFile(os.path.join(RAMMON_PATH, "rammon.pid")) if try_systemd_stop(): logger.info("Stopping rammon daemon with systemd...") if lock.is_locked(): try: proc = Process(lock.read_pid()) proc.terminate() try: proc.wait(1) except TimeoutExpired: print("Rammon did not stop gracefully, killing it...") proc.kill() lock.break_lock() logger.info("Rammon stopped successfully") except NoSuchProcess: logger.warning( "Rammon was already stopped, but had not stopped cleanly.\n" "Breaking pidfile lock...") lock.break_lock() else: logger.error("Rammon is already stopped") sys.exit(1)
def main(): parser = get_parser() opts, args = parser.parse_args() load_config_or_exit(opts.configfile) if opts.check and opts.background: parser.error('--check --background makes no sense, how will you know the result?') if not opts.background: log_to_stream(sys.stderr, level=logging.DEBUG if opts.debug else logging.WARNING) return doit(opts) else: pidlockfile = PIDLockFile(PIDFILE) existing_pid = pidlockfile.read_pid() if existing_pid: if process_is_alive(existing_pid): sys.stderr.write('Another beaker-init process is running (pid %s)\n' % existing_pid) return 1 else: sys.stderr.write('Pid file %s exists but pid %s is dead, ' 'removing the pid file\n' % (PIDFILE, existing_pid)) pidlockfile.break_lock() with daemon.DaemonContext(pidfile=pidlockfile, detach_process=True): log_to_syslog('beaker-init') return doit(opts)
import daemon from daemon.pidfile import PIDLockFile import logging from logging import handlers logger = logging.getLogger("mylogger") logger.setLevel(logging.INFO) file_handler = handlers.RotatingFileHandler("log/daemon.log", maxBytes=(1024 * 1024 * 512), backupCount=3) logger.addHandler(file_handler) pidLockfile = PIDLockFile('.pid') if pidLockfile.is_locked(): print("running already (pid: %d)" % pidLockfile.read_pid()) exit(1) context = daemon.DaemonContext(pidfile=pidLockfile) logfile_fileno = file_handler.stream.fileno() context.files_preserve = [logfile_fileno] def main(): while True: time.sleep(0.5) with context: main_program()
import os, sys import bjoern import daemon # import logging from daemon.pidfile import PIDLockFile from kaqpay import app if __name__ == '__main__': current_dir = os.getcwd() pidfile = PIDLockFile(app.config.get('PID_FILE')) if pidfile.is_locked(): print("Running Already (pid: {})".format(pidfile.read_pid())) # can use logging but why make things complicated? stdout = open(app.config.get('STDOUT_LOG'), 'a+') stderr = open(app.config.get('STDERR_LOG'), 'a+') ctx = daemon.DaemonContext(working_directory=current_dir, umask=0o002, pidfile=pidfile, stdout=stdout, stderr=stderr) with ctx: bjoern.run(app, 'localhost', 9091)
if log_type == 'dir': shutil.rmtree(LogFile, ignore_errors=True) logger.logger.info('%s :: Removed Log File : %s', GetCurFunc(), LogFile) except Exception as e: logger.logger.info('%s :: Remove Log File Failure : %s', GetCurFunc(), e) logger.logger.info('dellog.py End ===================') if __name__ == "__main__": # logger = DaemonLogger("dellogLog") logger = DaemonLogger() app = App() daemon_runner = runner.DaemonRunner(app) daemon_runner.daemon_context.files_preserve = [logger.hdlr.stream.fileno()] try: daemon_runner.do_action() except LockTimeout: pidLockfile = PIDLockFile(app.pidfile_path) if pidLockfile.is_locked(): print "dellog running already (pid: %s)" % (pidLockfile.read_pid()) logger.logger.info("dellog running already (pid: %s)", pidLockfile.read_pid()) logger.logger.info('sys.exit') sys.exit() except runner.DaemonRunnerStopFailureError: print "dellog not running !!" sys.exit()
from TaskManager import TaskManager from db import MongoDataAdapter from time import sleep from daemon.pidfile import PIDLockFile import psutil def create_task_manager(db, max_tasks, scan_interval): # TaskManager instance. tm = TaskManager(mongo, max_tasks=max_tasks, scan_interval=scan_interval) return tm lock = PIDLockFile('pet.pid') if lock.is_locked() and not lock.i_am_locking(): if not psutil.pid_exists(lock.read_pid()): print 'The pidlock is hold by a void process. Breaking it.' lock.break_lock() else: print 'Another process is holding pidlock. Exit.' exit() print 'Acquiring pidlock.' lock.acquire(timeout=5) # TODO Load config. mongo = MongoDataAdapter.create_adapter('GatesPet') max_tasks = 50 scan_interval = 10 process_interval = 120 tm = create_task_manager(mongo, max_tasks, scan_interval)