Take periodic screenshots in Ubuntu with scrot and cron

I had to log my hourly effort in my last gig (stupid policy IMO; what am I, a factory worker?) but since I’m not working on Windows, I couldn’t use ManicTime to do the tracking for me.

Taking a cue from oDesk, I decided to make a simple script that takes screenshots of my desktop every few minutes so that I could just review them at the end of the day. For this I used scrot for taking the screenshots and cron for the scheduling.

First install scrot:

$ sudo apt-get install scrot

Then write a script, say /path/to/home/scrot.sh:

#!/bin/sh
LOCATION="$(date +/path/to/home/Pictures/shots/%Y/%m/%d)"
mkdir -p $LOCATION
cd $LOCATION
DISPLAY=:0 scrot '%Y-%m-%d-%H%M.jpg' -q 20

This script will take a low quality screenshot of the desktop and put it in the Pictures/shots folder separated by date.

Set the permissions with chmod:

$ chmod u+x /path/to/home/scrot.sh

You can now test this by running:

$ /path/to/home/scrot.sh

Now time to add the script as a cron job. There are a lot of cron tutorials out there, but to summarize what you need to do, here are the basic steps:

Run crontab -e to edit the cron table file for your current user.

Add a line that will execute scrot.sh whenever you want it to run. For example, here’s the entry for running the script every three minutes on Monday to Friday:

*/3 * * * 1-5 /path/to/home/scrot.sh