diff --git a/telegram-notify-daemon b/telegram-notify-daemon index aecfe0e..7876758 100755 --- a/telegram-notify-daemon +++ b/telegram-notify-daemon @@ -12,6 +12,7 @@ import os import argparse import logging import atexit +import signal from telepot.loop import MessageLoop from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler @@ -167,6 +168,15 @@ def checkFiles(): processFile("%s/%s" % (spool_dir,f) ) +def initSignals(): + signal.signal(signal.SIGINT, exitGracefully) + signal.signal(signal.SIGTERM, exitGracefully) + + +def exitGracefully(signum, frame): + global shutdown + shutdown = True + def setDefaults(): global configFile global defaults @@ -300,7 +310,7 @@ def initBot(): observer.schedule(FilesChangedHandler(), path=spool_dir) observer.start() - +initSignals() setDefaults() parseCmdLine() readConfigFile() @@ -311,8 +321,13 @@ initBot() daemon = configParser.get('general', 'daemon') +shutdown = False while 1: checkFiles() if re.match('false', daemon, re.IGNORECASE): sys.exit(0) - time.sleep(10) + for x in range(0, 40): + if shutdown: + logger.info('Daemon shutdown requested. Exiting...') + sys.exit(0) + time.sleep(0.25)