def checker(): if ioloop._stopped: return if not motionctl.running() and motionctl.started() and config.has_local_enabled_cameras(): try: logging.error("motion not running, starting it") motionctl.start() except Exception as e: logging.error("failed to start motion: %(msg)s" % {"msg": unicode(e)}, exc_info=True) ioloop.add_timeout(datetime.timedelta(seconds=settings.MOTION_CHECK_INTERVAL), checker)
def start(): global _started _started = True if running() or not config.has_local_enabled_cameras(): return logging.debug('starting motion') program = find_motion() if not program: raise Exception('motion executable could not be found') program, version = program # @UnusedVariable logging.debug('using motion binary "%s"' % program) motion_config_path = os.path.join(settings.CONF_PATH, 'motion.conf') motion_log_path = os.path.join(settings.LOG_PATH, 'motion.log') motion_pid_path = os.path.join(settings.RUN_PATH, 'motion.pid') args = [program, '-c', motion_config_path, '-n', '-d'] if settings.LOG_LEVEL == logging.DEBUG: args.append('9') else: args.append('1') log_file = open(motion_log_path, 'w') process = subprocess.Popen(args, stdout=log_file, stderr=log_file, close_fds=True, cwd=settings.CONF_PATH) # wait 2 seconds to see that the process has successfully started for i in xrange(20): # @UnusedVariable time.sleep(0.1) exit_code = process.poll() if exit_code is not None and exit_code != 0: raise Exception('motion failed to start') pid = process.pid # write the pid to file with open(motion_pid_path, 'w') as f: f.write(str(pid) + '\n') _disable_initial_motion_detection()