Hi all, I have written a daemon as part of a larger project, if it recieves a SIGHUP signal it needs to re-read its config file. It works in that I get 'signal HUP detected, re-reading config file' in syslog but then the script exits ... mmm .... first time I have used signal catching ... thought it would continue its infinate loop. Any idea anyone ?
Cheers Dave import os, sys, time, signal, ConfigParser, kmotion_logger class Kmotion_Hkd2: def __init__(self): self.snap_init = [ 0 for i in xrange(16) ] self.snap_count = [ 0 for i in xrange(16) ] self.video_dir = '' self.motion_feeds = '' self.logger = kmotion_logger.Logger('kmotion_hdk2', 'DEBUG') signal.signal(signal.SIGHUP, self.signal_hup) self.read_config() def start_daemon(self): self.logger.log('daemon starting ...', 'DEBUG') """" Start the house keeping 2 daemon """ while(True): target_date = time.strftime('%Y%m%d') # Scan the feeds for feed in xrange(self.motion_feeds): target_tmp = '%s/%s/%02i/tmp/' % (self.video_dir, target_date, (feed + 1)) target_video = '%s/%s/%02i/video/' % (self.video_dir, target_date, (feed + 1)) # If target_date or target_tmp don't exist they will shortly as motion auto generates them # If target_video doesn't exist, could be just no motion so add dir if not(os.path.isdir(self.video_dir + '/' + target_date)) or not(os.path.isdir(target_tmp)): continue if not(os.path.isdir(target_video)): os.mkdir(target_video) jpeg_list = os.listdir(target_tmp) jpeg_list.sort() while (len(jpeg_list) >= 3): jpeg = jpeg_list[:1][0] self.snap_count[feed] = self.snap_count[feed] - 1 if self.snap_count[feed]: # Still counting down the snap_count[], so delete the snapshot self.logger.log('deleteing snapshot %s' % (target_tmp + jpeg), 'DEBUG') os.remove(target_tmp + jpeg) else: # snap_count[] = 0, reset it & do something with the snapshot self.snap_count[feed] = self.snap_init[feed] if os.path.isdir(target_video + jpeg[:-4]) or not(self.snap_init[feed]): # If there is a video file dir or if snap_init[feed] = 0, we dont need a snapshot so remove it self.logger.log('remove snapshot due to video clash %s/tmp/%s' % (self.video_dir, jpeg), 'DEBUG') os.remove(target_tmp + jpeg) else: # No video file dir, move the snapshot self.logger.log('rename %s %s' % (target_tmp + jpeg, target_video + jpeg), 'DEBUG') os.rename(target_tmp + jpeg, target_video + jpeg) jpeg_list = jpeg_list[1:] time.sleep(2) def read_config(self): """ Read config file from '~/.kde/share/apps/kmotion/kmotion.rc' """ parser = ConfigParser.SafeConfigParser() parsed = parser.read(os.path.expanduser('~/.kde/share/apps/kmotion/kmotion.rc')) if parsed[0][-10:] != 'kmotion.rc': emsg = 'Can\'t open config file %s - killing motion & all daemon processes' % (parsed[0][-10:]) self.logger.log(emsg, 'CRIT') self.kill_daemons() sys.exit() try: self.video_dir = parser.get('misc', 'video_dir') self.motion_feeds = 0 # Get ready to count the live feeds for i in xrange(0, 16): self.snap_init[i] = int(parser.get('feed%s' % (str(i)), 'snapshot_interval')) if parser.get('feed%s' % (str(i)), 'live') == "yes" : self.motion_feeds = self.motion_feeds + 1 except: emsg = 'Corrupt config %s - Killing motion & all daemons processes' % (sys.exc_info()[1]) self.logger.log(emsg, 'CRIT') self.kill_daemons() sys.exit() for i in xrange(16): # Force an immediate snapshot on all feeds self.snap_count[i] = 1 def kill_daemons(self): """ Kill motion & all daemons """ os.system('killall -q motion') os.system('pkill -f \'python.+kmotion_hkd1.py\'') def signal_hup(self, signum, frame): """ Re-read the config file on SIGHUP """ self.logger.log('signal HUP detected, re-reading config file', 'DEBUG') self.read_config() if __name__ == '__main__': Hkd2 = Kmotion_Hkd2() Hkd2.start_daemon() -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor