From 5da5a0e80ddf65e5d086fa7918aed11d99cbceff Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Wed, 18 Jul 2012 21:26:07 +0200 Subject: [PATCH] =?utf8?q?C'est=20cool=20d'=C3=AAtre=20daemonizable?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- saturnin.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 5 deletions(-) diff --git a/saturnin.py b/saturnin.py index a99fb15..87e5baf 100755 --- a/saturnin.py +++ b/saturnin.py @@ -6,8 +6,6 @@ # Un bot IRC pour remplacer le canard. # parce que le canard, c'est le bien et que braice ne pong pas -import irclib -import ircbot import threading import random import time @@ -15,8 +13,15 @@ import socket, ssl, json import pickle import re import os +import signal +import sys from commands import getstatusoutput as ex +# Oui, j'ai recodé ma version d'irclib pour pouvoir rattrapper les SIGHUP +sys.path.insert(0, "/home/vincent/scripts/python-myirclib") +import irclib +import ircbot + # on récupère la config import config @@ -577,18 +582,48 @@ class Saturnin(ircbot.SingleServerIRCBot): def _getnick(self): return self.serv.get_nickname() nick=property(_getnick) + + def start_as_daemon(self, outfile): + sys.stderr = Logger(outfile) + self.start() + + +class Logger(object): + """Pour écrire ailleurs que sur stdout""" + def __init__(self, filename="saturnin.full.log"): + self.filename = filename + + def write(self, message): + f = open(self.filename, "a") + f.write(message) + f.close() if __name__=="__main__": import sys if len(sys.argv)==1: - print "Usage : saturnin.py [--debug]" + print "Usage : saturnin.py [--debug] [--no-output] [--daemon [--pidfile]] [--outfile]" + print " --outfile sans --no-output ni --daemon n'a aucun effet" exit(1) serveur=sys.argv[1] + if "--daemon" in sys.argv: + thisfile = os.path.realpath(__file__) + thisdirectory = thisfile.rsplit("/", 1)[0] + os.chdir(thisdirectory) + daemon = True + else: + daemon = False if "debug" in sys.argv or "--debug" in sys.argv: debug=True else: debug=False + if "--no-output" in sys.argv or "--daemon" in sys.argv: + outfile = "/var/log/bots/saturnin.full.log" + for arg in sys.argv: + arg = arg.split("=") + if arg[0].strip('-') in ["out", "outfile", "logfile"]: + outfile = arg[1] + sys.stdout = Logger(outfile) if "--quiet" in sys.argv: config.debug_stdout=False serveurs={"a♡":"acoeur.crans.org","acoeur":"acoeur.crans.org","acoeur.crans.org":"acoeur.crans.org", @@ -598,5 +633,22 @@ if __name__=="__main__": except KeyError: print "Server Unknown : %s"%(serveur) exit(404) - bot = Saturnin(serveur,debug) - bot.start() + saturnin = Saturnin(serveur,debug) + if daemon: + child_pid = os.fork() + if child_pid == 0: + os.setsid() + saturnin.start_as_daemon(outfile) + else: + # on enregistre le pid de saturnin + pidfile = "/var/run/bots/saturnin.pid" + for arg in sys.argv: + arg = arg.split("=") + if arg[0].strip('-') in ["pidfile"]: + pidfile = arg[1] + f = open(pidfile, "w") + f.write("%s\n" % child_pid) + f.close() + else: + saturnin.start() + -- 2.39.2