Take A Break

What it is: An hourly reminder to rest my eyes and neck from computer work. In the form of irresistable cat photos that pop up on my screen.

How it's built: The core is a 4-line shell script that picks at random from a set of cat photos. A cron job runs the script at the beginning of each hour.

How to use it: Set the cron job for whatever interval you want. Then just feed your script with cat photos.

THE STORY

The problem was I started having tremors in my head and neck. Turns out I have a condition called Cervical Dystonia, and my new lifestyle of sitting for hours at a computer under intense focus was...not good. I needed a recurring reminder to stand up, walk around the block, or just take a break for 5 minutes. And it needed to be something that was hard to dismiss.

My solution was to bring my cat to work. Or rather, to build a cron job that showed me a photo of my cat every so often, to remind me to give my body a break. I had never set up a cron job on my computer, but it's not hard. If you aren't familiar, cron is a utility available on most systems which allows you to run jobs at set intervals. In this context, a 'job' can be almost anything.

Most linux distributions have the crontab command, which makes setting a cron job even easier. Running crontab -e opens a document in your default editor, and you just add your cron syntax along with the command, script, or path to a script that you want to run as a job. For me, this looks like:

0 * * * * /home/dylan/take_a_break.sh

Where 0 * * * * is the cron syntax that means 'at the start of every hour' and /home/dylan/take_a_break.sh is the path to the script that I want to run. The script itself is very simple too:

!#/bin/bash

num=$(bash -c 'echo $RANDOM')
num=$((num % 7 + 1))
filepath="/home/dylan/Documents/reminder$num.jpg"

DISPLAY=:0 /usr/bin/eog $filepath

This bash script uses interpolation to add a random number between one and seven to a string filepath. It then feeds that string as an argument to the command /usr/bin/eog which is the executable path on my machine for Eye of GNOME, the basic image-viewing app for the linux desktop. For this script to work I need seven image files in my Documents folder named reminder1.jpg through reminder7.jpg. If I want to include more images in the roulette I just need to put them in the right folder with the right name (reminder8.jpg will be next), and then increment the divisor on line 4 of the script.

The most interesting piece of the design puzzle was generating the random number. $RANDOM is a function built in to Bash that returns a number ranging from 0 to 32767. Seems simple enough. But you can't just write num=$RANDOM. I tried that, and after about an hour of troubleshooting the script and having Eye of GNOME pop up with an 'image not found' error, I discovered that $RANDOM was not available in cron. Even though I was asking cron to run a bash script, it still didn't know how to interpret $RANDOM. The solution turned out to be more interpolation. By passing $RANDOM as a string to bash I can return the random number without cron having to know anything about how $RANDOM should be handled. Once that hurdle was cleared, it was on to the cat photos.

This little program is really satisfying to me. It's had a clear, positive impact on my body and significantly reduced the amount of tremors I'm having at work, AND it involves photos of my cat. Win win, in my book. And it was my toe-dip into the pool of cron jobs, giving me familiarity with a core utility that will be a very useful tool for future programs. Thanks for reading!