From 3a5e72213356935c6fe47c58d78bbff1399c0d4e Mon Sep 17 00:00:00 2001 From: root Date: Wed, 10 Sep 2014 23:29:32 +0200 Subject: [PATCH] [common.py,daemon.py,initscript] Ecriture du daemon crans_ticket --- common.py | 6 ++++++ daemon.py | 58 ++++++++++++++++++++++++++++++++++++++++++++---------- initscript | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 10 deletions(-) create mode 100644 common.py create mode 100755 initscript diff --git a/common.py b/common.py new file mode 100644 index 0000000..cc7818f --- /dev/null +++ b/common.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +#-*- coding: utf-8 -*- +PIDFILE = '/var/run/daemon.pid' +USER='crans_ticket' +GROUP='adm' + diff --git a/daemon.py b/daemon.py index 6576cee..0405aaa 100755 --- a/daemon.py +++ b/daemon.py @@ -1,18 +1,56 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- +from __future__ import print_function +import sys,os,pwd,grp +import common import pika import json import dump import config -conn = pika.BlockingConnection(config.PARAMS) -ch = conn.channel() -ch.queue_declare(queue='CransTicket') -def callback(ch, method, properties, body): - print " [x] Received %r" % (body,) - dump.print_liste(json.loads(body)) +def run(): + conn = pika.BlockingConnection(config.PARAMS) + ch = conn.channel() + ch.queue_declare(queue='CransTicket') + def callback(ch, method, properties, body): + print (" [x] Received %r" % (body,)) + dump.print_liste(json.loads(body)) - -ch.basic_consume(callback, queue='CransTicket', no_ack=True) -ch.start_consuming() -conn.close() + ch.basic_consume(callback, queue='CransTicket', no_ack=True) + ch.start_consuming() + conn.close() +# fork en arrière plan + pidfile + +if __name__ == "__main__": + # do the UNIX double-fork magic, see Stevens' "Advanced + # Programming in the UNIX Environment" for details (ISBN 0201563177) + try: + pid = os.fork() + if pid > 0: + # exit first parent + sys.exit(0) + except OSError, e: + print("fork #1 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr) + sys.exit(1) + + # decouple from parent environment + os.chdir("/") #don't prevent unmounting.... + os.setsid() + os.umask(0) + + # do second fork + try: + pid = os.fork() + if pid > 0: + # exit from second parent, print eventual PID before + #print "Daemon PID %d" % pid + open(common.PIDFILE, 'w').write("%d" % pid) + sys.exit(0) + except OSError, e: + print("fork #2 failed: %d (%s)" % (e.errno, e.strerror),file=sys.stderr) + sys.exit(1) + + # start the daemon main loop + run() + diff --git a/initscript b/initscript new file mode 100755 index 0000000..7b9e6ae --- /dev/null +++ b/initscript @@ -0,0 +1,53 @@ +#! /bin/sh +### BEGIN INIT INFO +# Provides: crans_ticket +# Required-Start: $remote_fs +# Required-Stop: $remote_fs +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: crans_ticket +# Description: Impression thermique +### END INIT INFO + +NAME=crans_ticket +PIDF=/var/run/$NAME.pid +BIN_PATH=/usr/local/cransticket +#ARGS=root@crans.org +PATH=/sbin:/bin:/usr/sbin:/usr/bin +BIN="$BIN_PATH/daemon.py" +DESCR="Script premettant l'impression sur oie" + +. /lib/lsb/init-functions + +set -e + +case "$1" in + start) + echo -n "Démarrage de $NAME" + /sbin/start-stop-daemon --start --quiet --pidfile $PIDF --exec $BIN +#$ARGS + echo "." + ;; + stop) + echo -n "Arrêt de $NAME" + /sbin/start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDF + echo "." + ;; + + restart) + echo -n "Redémarrage (arrêt) de $NAME" + /sbin/start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile $PIDF + /bin/sleep 1 + echo -n "Redémarrage (démarrage) de $NAME" + /sbin/start-stop-daemon --start --quiet --pidfile $PIDF --exec $BIN +#$ARGS + echo "." + ;; + + *) + echo "Usage: /etc/init.d/$NAME {start|stop|restart}" + exit 1 +esac + +exit 0 +