Scheduling tasks with Cron

Setting up our Unix machines to periodically run commands at set intervals of time thanks to Cron and crontab.

By Luke Whitehouse on

DevOps

Tutorial

So you've just been asked to schedule a script to run at a predetermined time every day and haven't got a clue where to start? Cron can help. The Cron daemon is an ever-present process on your Unix based server that allows the administrator to schedule a command to run at a certain time of the day/week/month, whatever you need. Within this post, we'll arm ourselves with the knowledge to do exactly that.

Please keep in mind as this post deals solely with the Cron daemon, and as such assumes you are running a Unix based machine. For Windows, you won't have access to Cron and will need to look at schtasks that comes native to that Operating System.

Interacting with the Cron daemon

First things first, we need a way to access Cron and add some tasks for it to schedule. In order to do this, most modern systems come with the crontab CLI baked in. crontab, short for Cron table, is a configuration file that allows us to view and update our scheduled tasks, known as Cron jobs.

Whenever I'm learning about a new command the first thing I do is open the command's manual to see what's on offer. Pick your favourite terminal application and run the man command for crontab, which searches and displays a CLI's documentation if you have it.

$ man crontab

You'll see we have four flags to utilise when executing crontab. These are as followed:

-u    Specify the name of the user whose crontab is to be tweaked.  If this option is not given, crontab examines ``your'' crontab, i.e., the crontab of the person executing the command.  Note that su(1) can confuse crontab and that if you are running inside of su(1) you should always use the -u option for safety's sake.

-l    Display the current crontab on standard output.

-r    Remove the current crontab.

-e    Edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.The specified editor must edit the file in place; any editor that unlinks the file and recreates it cannot be used.  After you exit from the editor, the modified crontab will be installed automatically.

For this tutorial, we only need to concern ourselves with two, -l and -e.

Starting with the basics, you can list out the current Cron jobs you have scheduled by using the -l flag.

$ crontab -l

Running that for your first time will show that you have no crontab.

crontab: no crontab for <username>

We can create new jobs by using the -e flag to edit the crontab file. Before we do that, it's worth mentioning that by default the -e flag will open up the crontab file within vim. If you're not familiar with this editor, I'd recommend using the following command which will set the default editor -e uses to nano.

$ export VISUAL=nano; crontab -e

Now run the crontab -e command so you can start editing it.

Editing your Crontab

Scheduling your commands through crontab requires a basic understanding of its time-based syntax.

* * * * * /path/to/file

The first 5 parameters define the time and date of when the command should run, followed by a 6th parameter which is the command(s) you'd like to run. Going back to the man CLI, we can get a rough idea of what values are needed.

$ man 5 crontab

For the lazy, here's the bits we're interested in:

field          allowed values
-----          --------------
minute         0-59
hour           0-23
day of month   1-31
month          1-12 (or names, see below)
day of week    0-7 (0 or 7 is Sun, or use names)

Or, to put these in order:

<minute> <hour> <day of month> <month> <day of week> <your command to run>

Whenever you leave a field with an asterisk in its place you're essentially telling Cron you want the command to run at every value of that field, for example, "every day of the week" or "every minute of the day".

Useful examples of Cron Jobs

With the syntax out of the way, here are a few varied examples of Cron Jobs in the wild.

Run every hour

Your command will run every hour, on the hour. Change the 0 to another minute between 1-59 if you'd like it to run every hour at X minute.

0 * * * * <your command to run>

Run at 6am and 7pm every day

Commas are used to separate multiple instances of a field, and all values are based on the 24-hour clock.

0 6,19 * * * <your command to run>

Run on the 2nd of every month at 6:30am

Values can be combined to create more verbose schedules.

30 6 2 * * <your command to run>

Run hourly in business hours only

Ranges of values can be created using a hyphen

0 9-6 * * * <your command to run>

Run every 3 hours on every Sunday of every month

Values can have "steps" in them using the divide symbol to define a recurring amount of said value.

0 */3 * * 7 <your command to run>

That's a basic run down on scheduling Cron Jobs with crontab, along with a few examples to get you started. Got any burning examples of cron jobs you've found invaluable in the past? I'd love to hear about them in the comments section below or on Twitter.

Until next time 

Follow us on Twitter, Facebook or Github.
© Copyright 2021 Assortment.
Created and maintained by Luke Whitehouse.