Skip to content

Instantly share code, notes, and snippets.

@bytepossum
Created September 5, 2015 14:20
Show Gist options
  • Save bytepossum/12d7eb59db8f19d8524c to your computer and use it in GitHub Desktop.
Save bytepossum/12d7eb59db8f19d8524c to your computer and use it in GitHub Desktop.
Bash script for cron to check if motion is running and free disk space
#!/bin/bash
SERVICE='motion'
RUN_COMMAND='nice -n 10 /usr/local/bin/motion &'
LOGFILE=/home/pi/motion/motion.log
VOPATH=/media/data/motion # video output directory
LIMIT=90 # maximum disk usage
if [ ! $(pidof $SERVICE) ]
then
# process not running, starting
echo "[!!!] [$(date +%c)] check_motion.sh - $SERVICE is not running, starting '$RUN_COMMAND'" >> $LOGFILE
$($RUN_COMMAND)
fi
if [ $(df -P $VOPATH | awk '{ gsub("%",""); capacity = $5 }; END { print capacity }') -gt $LIMIT ]; then
echo "Free space is less than $LIMIT, removing 10 oldest files."
cd $VOPATH
rm $(ls -tr | awk 'NR<10')
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment