From 563a7b89ae8f20fcb6abcee98ff03990e2693010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Erard?= Date: Thu, 18 Aug 2011 00:08:47 +0200 Subject: [PATCH] Add option --pidfile to create a pid file when running as a daemon. --- Headphones.py | 7 +++++-- headphones/__init__.py | 17 +++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) mode change 100644 => 100755 Headphones.py mode change 100644 => 100755 headphones/__init__.py diff --git a/Headphones.py b/Headphones.py old mode 100644 new mode 100755 index 4487df68..51c0a2dc --- a/Headphones.py +++ b/Headphones.py @@ -34,6 +34,7 @@ def main(): parser.add_argument('--datadir', help='Specify a directory where to store your data files') parser.add_argument('--config', help='Specify a config file to use') parser.add_argument('--nolaunch', action='store_true', help='Prevent browser from launching on startup') + parser.add_argument('--pidfile', help='Create a pid file (only relevant when running as a daemon)') args = parser.parse_args() @@ -43,7 +44,9 @@ def main(): if args.daemon: headphones.DAEMON=True headphones.QUIET=True - + if args.pidfile : + headphones.PIDFILE = args.pidfile + if args.datadir: headphones.DATA_DIR = args.datadir else: @@ -75,7 +78,7 @@ def main(): if headphones.DAEMON: headphones.daemonize() - + # Force the http port if neccessary if args.port: http_port = args.port diff --git a/headphones/__init__.py b/headphones/__init__.py old mode 100644 new mode 100755 index b31968bd..f4df0246 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -22,6 +22,7 @@ SIGNAL = None QUIET = False DAEMON = False +PIDFILE= None SCHED = Scheduler() @@ -346,9 +347,13 @@ def daemonize(): os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) - - logger.info('Daemonized to PID: %s' % os.getpid()) - + + pid = os.getpid() + logger.info('Daemonized to PID: %s' % pid) + if PIDFILE: + logger.info('Writing PID %s to %s' % (pid, PIDFILE)) + file(PIDFILE, 'w').write("%s\n" % pid) + def launch_browser(host, port, root): if host == '0.0.0.0': @@ -558,7 +563,11 @@ def shutdown(restart=False, update=False): versioncheck.update() except Exception, e: logger.warn('Headphones failed to update: %s. Restarting.' % e) - + + if PIDFILE : + logger.info ('Removing pidfile %s' % PIDFILE) + os.remove(PIDFILE) + if restart: logger.info('Headphones is restarting...') popen_list = [sys.executable, FULL_PATH]