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 start(self): lock_file = PIDLockFile(self._file_name) if lock_file.is_locked(): print("Already run this daenon.") else: with daemon.DaemonContext(pidfile=lock_file): # TODO : Add code about error and exception. self.run(self._path)
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)
import time 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)
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)