Pages

Wednesday, 24 October 2012

script to take a photo of my screen every five minute



sudo apt-get install scrot
#!/bin/bash
# ^This first line just tells linux which script language to use.
# We're using bash
# Because this script runs as a Cron-job, it runs as root and doesn't
# necessarily know which user you're logged in as.  For me, this says "use
# the default display, ie the first person logged in on the computer"
DISPLAY=:0
HOME=/home/vose
export DISPLAY
export HOME
# Create the screenshots directory in case it doesn't exist yet
mkdir -p /home/vose/Pictures/Screenshots/
# Delete any screenshots more than 7 days old.
find /home/vose/Pictures/Screenshots/ -type f -mtime +7 -delete
# Launch the browser (I use firefox for normal, so I'll use chrome for this)
# The "sudo -u vose ..." means "run the command as user `vose`". You'll
# You'll need to put your username in there...
# The "&" at the end means 'launch in the background', so the script
# can keep going.
echo "Opening the browser"
sudo -u vose chromium-browser http://google.com &
# This records the ProcessID (pid) of the last opened program
# (chromium) so we can kill it later
pid=$!
# Wait 10 seconds for the browser to open and page to load
# On a slow computer/connection/webpage you may need to wait longer
sleep 10
# Take the screenshot using scrot.  Save it to this file
scrot /home/vose/Pictures/Screenshots/screenshot_`date +%F-%H-%M-%S`.jpg
echo "Created screenshot_`date +%F-%H-%M-%S`.jpg"
# Kill the browser
kill $pid
Before you try go too far, stop and see if this works.
First, save the script above as takeScreenshot.sh.
Then from a terminal, type chmod +x takeScreenshot.sh (Chmod changes the permissions on a file, and '+x' means we're giving it permission to "execute" the script.)
Run ./takeScreenshot.sh from a command line - this will run the script manually. See if the file shows up in your pictures folder after doing this.
If that worked, we need to now add it to Cron.
At a command line, type sudo crontab -e. (If it asks you which editor, choose Nano. If you haven't used it before, ask around for some help).
In the file, you want to add a line at the bottom, like this:
# m h dom mon dow command
*/5 * * * * /home/vose/Scripts/takeScreenshot.sh
What this says:
Every 5 minutes
Every hour, every day-of-month, every month, every day-of-week
Run "/home/vose/Scripts/takeScreenshot.sh"

No comments:

Post a Comment