Ubuntu Crontab: How to Make a Scheduled Task run every couple of Minutes CodeUnit 01 JUN 2011

As a web developer you will always find a use for a scheduled job, something that runs in the background every now and then and does some sort of batch job to make life easier for someone interacting with your portal. If you are running a Ubuntu linux server, then the scheduled task (or scheduled job) becomes a “cron job”, and the crontab becomes your friend. So let’s create a cron job that runs every couple of minutes then, shall we?

First, access your crontab, using elevated privileges if you want (note that sudo crontab and crontab are actually two different job managers) with:

sudo crontab -e

Faced with the default text editor open up in front of you, you’ll remember that the linux crontab format is as such: [minute] [hour] [day of month] [month] [day of week] [command] and that asterisk (*) means every possible value.

Now given the command “/home/me/check-db-status”, we could schedule a job to run for every minute by simply inputting

* * * * * /home/me/check-db-status

However, this is pretty harsh on your poor server, so let’s say run it after every 20 minute interval. Our crontab entry now changes to:

*/20 * * * * /home/me/check-db-status

A small change, but effective. In this way you can set something to run every couple of minutes, say after every 5 (*/5) or 12 (*/12) minutes.


(But note that you can’t run something every couple of seconds because the lowest available unit is a minute!)

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.