From ce4b594f65cc448d5b7dc0df4de64a17ba70f651 Mon Sep 17 00:00:00 2001 From: James Vasile Date: Fri, 16 Dec 2011 20:54:01 -0500 Subject: [PATCH] daemonize --- plinth.py | 24 ++++++++++++++++++++++-- share/init.d/plinth | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100755 share/init.d/plinth diff --git a/plinth.py b/plinth.py index 6f350097c..7a60adcda 100755 --- a/plinth.py +++ b/plinth.py @@ -1,7 +1,6 @@ #!/usr/bin/env python -# Start listener, just for testing -import os, sys +import os, sys, argparse #import logging from gettext import gettext as _ import cfg @@ -9,6 +8,10 @@ if not os.path.join(cfg.file_root, "vendor") in sys.path: sys.path.append(os.path.join(cfg.file_root, "vendor")) import cherrypy +from cherrypy import _cpserver +from cherrypy.process.plugins import Daemonizer +Daemonizer(cherrypy.engine).subscribe() + import plugin_mount from util import * from logger import Logger @@ -96,7 +99,24 @@ tools.staticfile.on = True tools.staticfile.filename = "{fileroot}/static/theme/favicon.ico" """.format(fileroot=cfg.file_root)) +def parse_arguments(): + parser = argparse.ArgumentParser(description='Plinht web interface for the FreedomBox.') + parser.add_argument('--pidfile', default="", + help='specify a file in which the server may write its pid') + args=parser.parse_args() + if args.pidfile: + cfg.pidfile = args.pidfile + def setup(): + parse_arguments() + + try: + if cfg.pidfile: + from cherrypy.process.plugins import PIDFile + PIDFile(cherrypy.engine, cfg.pidfile).subscribe() + except AttributeError: + pass + os.chdir(cfg.file_root) cherrypy.config.update({'error_page.404': error_page_404}) cherrypy.config.update({'error_page.500': error_page_500}) diff --git a/share/init.d/plinth b/share/init.d/plinth new file mode 100755 index 000000000..f5df9b712 --- /dev/null +++ b/share/init.d/plinth @@ -0,0 +1,38 @@ +#!/bin/bash +# This file is /etc/init.d/plinth +DAEMON=/usr/bin/plinth.py +PID_FILE=/var/run/plinth.pid + +start_plinth (){ + if [ -f $PID_FILE ]; then + echo Already running with a pid of `cat $PID_FILE`. + else + $DAEMON --pidfile=$PID_FILE + fi +} + +stop_plinth () { + if [ -f $PID_FILE ]; then + kill -15 `cat $PID_FILE` + rm -rf $PID_FILE + else + echo "No pid file at $PID_FILE suggests plinth is not running." + fi +} + +test -x $DAEMON || exit 0 +case "$1" in + start) + echo "Starting Plinth." + start_plinth + ;; + stop) + echo "Stoping Plinth." + stop_plinth + ;; + restart) + $0 stop + $0 start + ;; + +esac