UPDATE (06/22/11): Modified the below script to allow two arguments, start time and end time, so there aren’t useless screenshots taken when I’m not at work.
UPDATE 2 (06/22/11): Also added a condition to not take screenshots during the weekend.
Place the script below in ~/scripts, chmod 700 the new file you create (take-screenshot.sh is what I called it), and then place in your crontab as such:
Tested in Fedora 15, will require some modification to work in Ubuntu also …
[user@system scripts]$ crontab -l
*/5 * * * * ~/scripts/take-screenshot.sh 0930 1830 # 2>&1 | tee -a ~/screenshots/script.log.$$
#!/bin/bash
export PATH=/usr/bin:/bin
STARTTIME=${1}
ENDTIME=${2}
CURTIME=`date +%H%M`
TODAY=`date +%a`
if [ $# -eq 0 ]; then
echo "Usage: $0
echo "e.g. $0 0930 1730"
exit 5
elif [ ${CURTIME} -lt ${STARTTIME} -a ${CURTIME} -gt ${ENDTIME} ]; then
exit 1
elif [ ${TODAY} == "Sat" -o ${TODAY} == "Sun" ]; then
exit 1
fi
xhost +local:`whoami`
function makeDir() {
if [ ! -d ${1} ]; then
mkdir -p ${1}
fi
}
function checkSpace() {
USED=`df -k ${1} | awk '/.*[0-9]%/ {print $4}' | sed 's/%//g'`
if [ ! -x /usr/bin/zenity ]; then
yum install -y zenity >/dev/null 2>&1
fi
if [ ${USED} -gt 98 ]; then
zenity --warning --timeout=10 --text "Disk space is at ${USED}! Stuff will start to go wrong soon... :("
exit 5;
fi
}
if [ ! -x /usr/bin/scrot ]; then
yum install scrot -y >/dev/null 2>&1
fi
PDATE=`date +%F`
DIR=~/screenshots/${PDATE}
BACKDIR=~/screenshots/old
makeDir "${DIR}"
checkSpace "${DIR}"
DISPLAY=:0 scrot -q 30 '%Y-%m-%d@%H:%M.png' -e "mv \$f ${DIR}"
for i in `find ${DIR} -name '*.png' -mtime +1`; do
tar crfj ${BACKDIR}/old/oldscreens.tar.bz2 ${i}
rm -f ${i}
done