HostOnNet Blog

How to Automatically Take Screenshots in Ubuntu at Regular Interval

It is easy to take screenshots in Ubuntu by using the “Print Screen” button on your keyboard. If you want the system to take a screenshot automatically at a regular interval, here is a quick way.

Installing scrot

The tool that we are going to use is scrot. Scrot is a command line utility that allows you to capture screenshot from the terminal.

You can install scrot by running the following command in terminal.

sudo apt-get install scrot

Capturing screenshots at regular interval

To use Scrot to capture screenshots automatically at regular interval, all you need to do is to run the following command in the terminal:

while true; do scrot -d int  'filename.jpg|png' -e 'mv $f /file/path/to/store/screenshots'; done

Here are a few parameters that you need to change:

int – the number of seconds before each screenshot is taken
filename – the filename of the screenshot. You can use variables like %Y, %m, %d, %H, %M, %S $w, $h to denote the year, month, day, hour, minute, seconds, width and height respectively.
jpg|png – take the screenshot in either jpg or png format. Include only one, but not both.
file/path/to/store/screenshots – the location where you want to move the screenshots to

For example, if you want it to take a screenshot at every 5 seconds and save it to the Pictures folder. This is the command to use:

while true; do scrot -d 5 '%Y-%m-%d-%H:%M:%S.png' -e 'mv $f ~/Pictures/'; done

Note: Press “Ctrl + Z” to end the process.

Note: Scrot will take about 1 -2 second to complete each cycle. You might want to adjust the interval to compensate for this lag

After running it for 1 minute, this is what I found in my Pictures folder.

screenshot-saved

The above command will run the process forever until you stop it manually. If you want to get it to run for a certain count, say 100 loops, you can use the command below:

for i in {1..100}; do scrot -d 5 '%Y-%m-%d-%H:%M:%S.png' -e 'mv $f ~/Pictures/'; done

This will take 100 screenshots at an interval of 5 seconds.

Putting it in script

It is barely useful if you need to type the command everytime you want to run the process. The best way is to turn it into a script where you can run it anytime, everytime.

Open a text editor and paste the following commands:


#!/bin/bash

for i in {1..100}
do
scrot -d 5 '%Y-%m-%d-%H:%M:%S.png' -e 'mv $f ~/Pictures/';
done

Save the file as auto-screenshot.sh in your Home folder. Grant it executable permission:

chmod +x ~/auto-screenshot.sh

Now you can run the process by using the command in the terminal:

./auto-screenshot.sh

Automating the screen capturing process

If you want to schedule the screen capturing process to run at a certain time everyday, the fastest way is to set a cronjob.If you prefer a more graphical approach, Gnome Schedule is one good app that you can use, provided you are using the Gnome desktop.

For further automation, you can even use CuttleFish to trigger the screen capturing process when a condition is met.

What other ways do you use to automate screen capturing at regular interval?


Posted in Linux, Ubuntu

  • Kanagavalli

    Its really easy to take a screenshot through Print Screen button. But you provide the solution through which we take a screenshot automatically at a regular interval. Great post.