From 846d20376af79c769fd3b6f6c2b9a2025727d423 Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Wed, 18 Jul 2012 21:24:49 +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 --- deconnaisseur.py | 63 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/deconnaisseur.py b/deconnaisseur.py index 88fd8c3..eb70224 100755 --- a/deconnaisseur.py +++ b/deconnaisseur.py @@ -5,15 +5,21 @@ # Un bot IRC qui sort des déconnaissances -import irclib -import ircbot import threading import random import time import pickle import re +import signal +import sys from remplace_accents import remplace_accents +# 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 + + config_password="PatrickSébastien" config_pseudo="deconnaisseur" config_chanlist=["#bot","#flood"] @@ -619,23 +625,70 @@ class Deconnaisseur(ircbot.SingleServerIRCBot): self.last_activity[canal]=time.time() def is_active(self,canal): return time.time()-self.last_activity[canal] [--debug]" + print "Usage : deconnaisseur.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 serveurs={"a♡":"acoeur.crans.org","acoeur":"acoeur.crans.org","acoeur.crans.org":"acoeur.crans.org", "irc":"irc.crans.org","crans":"irc.crans.org","irc.crans.org":"irc.crans.org"} + if "--no-output" in sys.argv or "--daemon" in sys.argv: + outfile = "/var/log/bots/deconnaisseur.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) try: serveur=serveurs[serveur] except KeyError: print "Server Unknown : %s"%(serveur) exit(404) - deco=Deconnaisseur(serveur,debug) - deco.start() + deconnaisseur=Deconnaisseur(serveur,debug) + if daemon: + child_pid = os.fork() + if child_pid == 0: + os.setsid() + deconnaisseur.start_as_daemon(outfile) + else: + # on enregistre le pid de deconnaisseur + pidfile = "/var/run/bots/deconnaisseur.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: + deconnaisseur.start() \ No newline at end of file -- 2.39.2