Motion Security Camera

07 Nov 2014  Posted under: linux , security

Getting motion installed and working is ubuntu is pretty easy

sudo apt-get install motion

It works pretty well out of the box if you want to use it with a webcam, just try the following

sudo motion

and then look in your webbrowser at 127.0.0.1:8081. Note that by default you have to do it from the server.

Getting it configured properly is the tricky part. I needed to make a few adjustments in particular…

This is done by making the following adjustments to /etc/motion/motion.conf

daemon off
videodevice /dev/video1
ffmpeg_video_codec flv
target_dir /media/VIDEO
snapshot_filename %m-%d/%v-%Y%m%d%H%M%S-snapshot
jpeg_filename %Y-%m-%d/%H-%M-%S-%v-%q
movie_filename %Y-%m-%d/%H-%M-%S-%v
timelapse_filename %Y-%m-%d/%H-%M-timelapse
webcam_port 80
webcam_motion on
webcam_localhost off

After those changes, you can start motion manually (sudo motion) to check that everything was working right.

Getting motion to run as a service

First, enable the service by changing /etc/defaults/motion to the following

start_motion_daemon=yes

Second, the service will try to run as user motion instead of root, but that was a problem since I wanted to use port 80. I needed to edit the sysinit script at /etc/init.d/motion and look for the following line

if start-stop-daemon --start --oknode --exec $DAEMON -b --child motion ; then

Remove the --child motion part so the line looks like this:

if start-stop-daemon --start --oknode --exec $DAEMON -b ; then

Now you should be able to start motion as a service using

sudo service motion start

Cleaning up files

The final problem is that motion is going to start spitting out images, and will continue to do so until all the space on your machine has been used up. The recommended solution is to use a cron job.

Edit roots cron file using

sudo crontab -e

and add the following line to remove any files motion that are older then 3 days, everyday at 1am.

# m h dom mon dow command
0 1 * * * /usr/bin/find /media/VIDEO -mtime +2 -and -type f -and -name "*jpg" | xargs /bin/rm -f