[common.py,daemon.py,initscript] Ecriture du daemon crans_ticket
This commit is contained in:
parent
fb298f5d8d
commit
3a5e722133
3 changed files with 107 additions and 10 deletions
6
common.py
Normal file
6
common.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
#-*- coding: utf-8 -*-
|
||||
PIDFILE = '/var/run/daemon.pid'
|
||||
USER='crans_ticket'
|
||||
GROUP='adm'
|
||||
|
58
daemon.py
58
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()
|
||||
|
||||
|
|
53
initscript
Executable file
53
initscript
Executable file
|
@ -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
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue