Execute process only when away

As an improvement of streaming motion detection video to the cloud, it’d be great to do this only when I’m away. A simple bash script installed as a cron job can ping a cell phone every couple minutes, and if successful, make sure the motion process isn’t running. If the phone is unreachable, the process is started.

This is contingent on having the phone’s WiFi¬†feature on, telling the DHCP server to always assign it the same address, and setting power saving features to keep WiFi¬†on when in sleep mode. In time, I’ll know whether this heavily impacts battery lifetime.

Additionally, the whole function of this will only take place if the second video device, /dev/video1, is present.

#!/bin/bash
if [ ! -e /tmp/cam/archive ]; then
    mkdir /tmp/cam
    curlftpfs 172.31.1.1 /tmp/cam -o user=camera:password
fi

while :
do
    if [ -e /dev/video1 ]; then
        ping -c1 172.31.1.11 > /dev/null 2>&1
        if [ $? -eq 0 ]; then
            if [ -e /tmp/motion.pid ]; then
                kill `cat /tmp/motion.pid`
                rm /tmp/motion.pid
            fi
        else
            if [ ! -e /tmp/motion.pid ]; then
                motion &
                echo $! > /tmp/motion.pid
            fi
        fi
    fi
    sleep 120
done