diff --git a/Headphones.py b/Headphones.py old mode 100644 new mode 100755 index e75c60c4..ba494c51 --- a/Headphones.py +++ b/Headphones.py @@ -47,6 +47,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() @@ -56,7 +57,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: @@ -88,7 +91,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 fa05d1b1..9949986d --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -24,6 +24,7 @@ SYS_ENCODING = None QUIET = False DAEMON = False +PIDFILE= None SCHED = Scheduler() @@ -348,9 +349,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': @@ -560,7 +565,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]