Example #1
0
def start_daemons():
    """ 
    Check and start all the kmotion daemons

    args    : 
    excepts : 
    return  : none
    """ 
    
    logger.log('start_daemons() - starting daemons ...', 'DEBUG')
    kmotion_dir = load_rc()[0]
    
    p_objs = Popen('ps ax | grep kmotion_hkd1.py$', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    if p_objs.stdout.readline() == '':   
        Popen('nohup %s/core/kmotion_hkd1.py >/dev/null 2>&1 &' % kmotion_dir, shell=True) 
        logger.log('start_daemons() - starting kmotion_hkd1', 'DEBUG')

    p_objs = Popen('ps ax | grep kmotion_hkd2.py$', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    if p_objs.stdout.readline() == '':   
        Popen('nohup %s/core/kmotion_hkd2.py >/dev/null 2>&1 &' % kmotion_dir, shell=True)
        logger.log('start_daemons() - starting kmotion_hkd2', 'DEBUG')

    p_objs = Popen('ps ax | grep kmotion_fund.py$', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    if p_objs.stdout.readline() == '':   
        Popen('nohup %s/core/kmotion_fund.py >/dev/null 2>&1 &' % kmotion_dir, shell=True)
        logger.log('start_daemons() - starting kmotion_fund', 'DEBUG')
        
    p_objs = Popen('ps ax | grep kmotion_setd.py$', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    if p_objs.stdout.readline() == '':  
        Popen('nohup %s/core/kmotion_setd.py >/dev/null 2>&1 &' % kmotion_dir, shell=True)
        logger.log('start_daemons() - starting kmotion_setd', 'DEBUG')
        
    p_objs = Popen('ps ax | grep kmotion_ptzd.py$', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
    if p_objs.stdout.readline() == '': 
        Popen('nohup %s/core/kmotion_ptzd.py >/dev/null 2>&1 &' % kmotion_dir, shell=True)
        logger.log('start_daemons() - starting kmotion_ptzd', 'DEBUG')
    
    # check for a 'motion.conf' file before starting 'motion'
    if os.path.isfile('%s/core/motion_conf/motion.conf' % kmotion_dir):
            
        p_objs = Popen('/bin/ps ax | /bin/grep [m]otion\ -c', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
        if p_objs.stdout.readline() == '': 
            init_core.init_motion_out(kmotion_dir) # clear 'motion_out'
            Popen('while true; do test -z "$(pgrep -f \'^motion.+-c.*\')" -o -z "$(netstat -an | grep 8080)" && ( pkill -9 -f \'^motion.+-c.*\'; motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out & ); sleep 1; done &' % (kmotion_dir, kmotion_dir), shell=True)
            #Popen('while true; do killall -9 -q motion; motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out; sleep 1; done &' % (kmotion_dir, kmotion_dir), shell=True)
            #Popen('nohup motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out &' % (kmotion_dir, kmotion_dir), shell=True)
            logger.log('start_daemons() - starting motion', 'DEBUG')
            
    else:
        logger.log('start_daemons() - no motion.conf, motion not started', 'CRIT')
Example #2
0
def reload_motion_config():
    """ 
    Force motion to reload configs. The 'motion_reload_bug' flags whether a 
    SIGHUP is sufficient to reload motions configs or whether motion needs to be 
    stopped and restarted.

    Unfortunately motion appears not to look at its /dev/* files on receiving a 
    SIGHUP so a once connected device is assumed to be still there.
    
    args    : 
    excepts : 
    return  : none
    """

    rc = load_rc()
    kmotion_dir = rc[0]
    motion_reload_bug = rc[1]
    init_core.init_motion_out(kmotion_dir) # clear 'motion_out'
    if motion_reload_bug: # motion_reload_bug workaround
        trys = 0
        while True:
            
            trys += 1
            if trys < 20:
        	Popen('pkill -f \'.*motion.+-c.*\'', shell=True) # if motion hangs get nasty !
                #Popen('killall -q motion', shell=True)
            else: 
                logger.log('reload_motion_config() - resorting to kill -9 ... ouch !', 'DEBUG')
                #Popen('killall -9 -q motion', shell=True) # if motion hangs get nasty !
                Popen('pkill -9 -f \'.*motion.+-c.*\'', shell=True) # if motion hangs get nasty !
            
            p_objs = Popen('/bin/ps ax | /bin/grep [m]otion\ -c', shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
            stdout = p_objs.stdout.readline()
            if stdout == '': break
            
            time.sleep(1)
            logger.log('reload_motion_config() - motion not killed - retrying ...', 'DEBUG')
            
        logger.log('reload_motion_configs() - motion killed', 'DEBUG')
    
        # check for a 'motion.conf' file before starting 'motion'
        if os.path.isfile('%s/core/motion_conf/motion.conf' % kmotion_dir):
                
            logger.log('reload_motion_configs() - pausing for 2 seconds ...', 'DEBUG')
            time.sleep(2)
            init_core.init_motion_out(kmotion_dir) # clear 'motion_out'
            Popen('while true; do test -z "$(pgrep -f \'^motion.+-c.*\')" -o -z "$(netstat -an | grep 8080)" && ( pkill -9 -f \'^motion.+-c.*\'; motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out & ); sleep 1; done &' % (kmotion_dir, kmotion_dir), shell=True)
            #Popen('while true; do killall -9 -q motion; motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out; sleep 1; done &' % (kmotion_dir, kmotion_dir), shell=True)
            logger.log('reload_motion_configs() - restarting motion', 'DEBUG')
                
        else:
            logger.log('reload_motion_configs() - no motion.conf, motion not restarted', 'CRIT')
        
    else:        
        init_core.init_motion_out(kmotion_dir) # clear 'motion_out'
        os.popen('killall -s SIGHUP motion')
        logger.log('reload_motion_configs() - motion sent SIGHUP signal', 'DEBUG')
Example #3
0
def reload_motion_config():
    """ 
    Force motion to reload configs. The 'motion_reload_bug' flags whether a 
    SIGHUP is sufficient to reload motions configs or whether motion needs to be 
    stopped and restarted.

    Unfortunately motion appears not to look at its /dev/* files on receiving a 
    SIGHUP so a once connected device is assumed to be still there.
    
    args    : 
    excepts : 
    return  : none
    """

    rc = load_rc()
    kmotion_dir = rc[0]
    motion_reload_bug = rc[1]
    init_core.init_motion_out(kmotion_dir)  # clear 'motion_out'
    if motion_reload_bug:  # motion_reload_bug workaround
        trys = 0
        while True:

            trys += 1
            if trys < 4:
                Popen('killall -q motion', shell=True)
            else:
                logger.log(
                    'reload_motion_config() - resorting to kill -9 ... ouch !',
                    'DEBUG')
                Popen('killall -9 -q motion',
                      shell=True)  # if motion hangs get nasty !

            p_objs = Popen('/bin/ps ax | /bin/grep [m]otion\ -c',
                           shell=True,
                           stdin=PIPE,
                           stdout=PIPE,
                           stderr=PIPE,
                           close_fds=True)
            stdout = p_objs.stdout.readline().decode()
            if stdout == '': break

            time.sleep(1)
            logger.log(
                'reload_motion_config() - motion not killed - retrying ...',
                'DEBUG')

        logger.log('reload_motion_configs() - motion killed', 'DEBUG')

        # check for a 'motion.conf' file before starting 'motion'
        if os.path.isfile('%s/core/motion_conf/motion.conf' % kmotion_dir):

            logger.log('reload_motion_configs() - pausing for 2 seconds ...',
                       'DEBUG')
            time.sleep(2)
            init_core.init_motion_out(kmotion_dir)  # clear 'motion_out'
            Popen(
                'nohup motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out &'
                % (kmotion_dir, kmotion_dir),
                shell=True)
            logger.log('reload_motion_configs() - restarting motion', 'DEBUG')

        else:
            logger.log(
                'reload_motion_configs() - no motion.conf, motion not restarted',
                'CRIT')

    else:
        init_core.init_motion_out(kmotion_dir)  # clear 'motion_out'
        os.popen('killall -s SIGHUP motion')
        logger.log('reload_motion_configs() - motion sent SIGHUP signal',
                   'DEBUG')
Example #4
0
def start_daemons():
    """ 
    Check and start all the kmotion daemons

    args    : 
    excepts : 
    return  : none
    """

    logger.log('start_daemons() - starting daemons ...', 'DEBUG')
    kmotion_dir = load_rc()[0]
    p_objs = Popen('ps ax | grep kmotion_hkd1.py$',
                   shell=True,
                   stdin=PIPE,
                   stdout=PIPE,
                   stderr=PIPE,
                   close_fds=True)
    if p_objs.stdout.readline().decode() == '':
        Popen('nohup %s/core/kmotion_hkd1.py >/dev/null 2>&1 &' % kmotion_dir,
              shell=True)
        logger.log('start_daemons() - starting kmotion_hkd1', 'DEBUG')

    p_objs = Popen('ps ax | grep kmotion_hkd2.py$',
                   shell=True,
                   stdin=PIPE,
                   stdout=PIPE,
                   stderr=PIPE,
                   close_fds=True)
    if p_objs.stdout.readline().decode() == '':
        Popen('nohup %s/core/kmotion_hkd2.py >/dev/null 2>&1 &' % kmotion_dir,
              shell=True)
        logger.log('start_daemons() - starting kmotion_hkd2', 'DEBUG')

    p_objs = Popen('ps ax | grep kmotion_fund.py$',
                   shell=True,
                   stdin=PIPE,
                   stdout=PIPE,
                   stderr=PIPE,
                   close_fds=True)
    if p_objs.stdout.readline().decode() == '':
        Popen('nohup %s/core/kmotion_fund.py >/dev/null 2>&1 &' % kmotion_dir,
              shell=True)
        logger.log('start_daemons() - starting kmotion_fund', 'DEBUG')

    p_objs = Popen('ps ax | grep kmotion_setd.py$',
                   shell=True,
                   stdin=PIPE,
                   stdout=PIPE,
                   stderr=PIPE,
                   close_fds=True)
    if p_objs.stdout.readline().decode() == '':
        Popen('nohup %s/core/kmotion_setd.py >/dev/null 2>&1 &' % kmotion_dir,
              shell=True)
        logger.log('start_daemons() - starting kmotion_setd', 'DEBUG')

    p_objs = Popen('ps ax | grep kmotion_ptzd.py$',
                   shell=True,
                   stdin=PIPE,
                   stdout=PIPE,
                   stderr=PIPE,
                   close_fds=True)
    if p_objs.stdout.readline().decode() == '':
        Popen('nohup %s/core/kmotion_ptzd.py >/dev/null 2>&1 &' % kmotion_dir,
              shell=True)
        logger.log('start_daemons() - starting kmotion_ptzd', 'DEBUG')

    # check for a 'motion.conf' file before starting 'motion'
    if os.path.isfile('%s/core/motion_conf/motion.conf' % kmotion_dir):

        p_objs = Popen('/bin/ps ax | /bin/grep [m]otion\ -c',
                       shell=True,
                       stdin=PIPE,
                       stdout=PIPE,
                       stderr=PIPE,
                       close_fds=True)
        if p_objs.stdout.readline().decode() == '':
            init_core.init_motion_out(kmotion_dir)  # clear 'motion_out'
            Popen(
                'nohup motion -c %s/core/motion_conf/motion.conf 2>&1 | grep --line-buffered -v \'saved to\' >> %s/www/motion_out &'
                % (kmotion_dir, kmotion_dir),
                shell=True)
            logger.log('start_daemons() - starting motion', 'DEBUG')

    else:
        logger.log('start_daemons() - no motion.conf, motion not started',
                   'CRIT')