added pidfile support

This commit is contained in:
Paolo Asperti 2018-01-15 21:58:15 +01:00
parent 6f844fcaee
commit c754373ae7
2 changed files with 25 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import sys
import os import os
import argparse import argparse
import logging import logging
import atexit
from telepot.loop import MessageLoop from telepot.loop import MessageLoop
from watchdog.observers import Observer from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler from watchdog.events import PatternMatchingEventHandler
@ -183,7 +184,8 @@ def setDefaults():
'daemon': False, 'daemon': False,
'token': '', 'token': '',
'pair_pin': '1234', 'pair_pin': '1234',
'spool_dir': '/var/spool/telegram-notify' 'spool_dir': '/var/spool/telegram-notify',
'pid_file': '/var/run/telegram-notify/telegram-notify.pid'
} }
@ -235,6 +237,26 @@ def initLogger():
logging.basicConfig(filename=logFile,level=logLevel,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s') logging.basicConfig(filename=logFile,level=logLevel,format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def checkPIDFile():
PIDFile = configParser.get('general', 'pid_file')
try:
pf = open(PIDFile,'r')
pid = int(pf.read().strip())
pf.close()
except (IOError, TypeError):
pid = None
if pid:
logger.error('pidfile %s already exist. Daemon already running?' % PIDFile)
sys.stderr.write('pidfile %s already exist. Daemon already running?' % PIDFile)
sys.exit(1)
atexit.register(delPIDFile)
open(PIDFile,'w+').write("%s\n" % str(os.getpid()))
logger.info('Created PIDfile %s' % PIDFile)
def delPIDFile():
PIDFile = configParser.get('general', 'pid_file')
os.remove(PIDFile)
logger.info('Removed PIDfile %s' % PIDFile)
def checkSpoolDir(): def checkSpoolDir():
spool_dir = configParser.get('general', 'spool_dir') spool_dir = configParser.get('general', 'spool_dir')
@ -281,6 +303,7 @@ setDefaults()
parseCmdLine() parseCmdLine()
readConfigFile() readConfigFile()
initLogger() initLogger()
checkPIDFile()
checkSpoolDir() checkSpoolDir()
initBot() initBot()

View File

@ -5,3 +5,4 @@ pair_pin = 1234
spool_dir = /var/spool/telegram-notify spool_dir = /var/spool/telegram-notify
log_file = /var/log/telegram-notify.log log_file = /var/log/telegram-notify.log
log_level = INFO log_level = INFO
pid_file = /var/run/telegram-notify/telegram-notify.pid