Cron Jobs: The Basics

Cron is a daemon process that runs at all times behind the scenes on Unix-like operating systems like Linux and OS X. And by “at all times” I mean whenever your operating system is running — even when you are logged out, for example.

Its function is to execute tasks on a regular schedule. Tasks can be anything that runs from the command-line, and can be scheduled to run on basically any schedule you can think of. For example, seven minutes past every hour, but only during the first Monday of February. It’s flexible.

Tasks (called Cron jobs) are scheduled by creating a crontab text file, which simply contains one-line declarations of each task and its schedule. Pass the crontab file into Cron, and the schedule is set. There’s no dynamic link created with the crontab file, so you could throw it away after importing if you like.

Scheduling A Task

Enough theory — let’s schedule a simple cron job. We’ll have cron add the output of the command “uptime” (which prints a short one-line summary of the current system status) into a text file, creating a log. The command to perform this action is as follows, saving output into a text file “uptime.log” which gets stored in your home folder:

uptime >> ~/uptime.log

As for the schedule — we want this task to run every minute. Specifying a schedule in a crontab text file is done via a series of five time entries, separated by whitespace (a space or a tab will do fine). This series of time entries takes the following form:

<minute> <hour> <month-day> <month> <week-day>

Noting the zero-indexing is important. <week-day> can use 0 or 7 to mean Sunday. I find it’s easiest to remember as all entries start at 1, except for minutes in hour which use zero values to mean midnight (00:00). Therefore the below is midnight, Monday Jan 1.

0 0 1 1 1

All 5 entries must be completed, and each can contain either an asterisk to mean “every,” or a number corresponding to, for example, the (5)th minute of the hour, the (18)th hour of the day, the (2)nd of the month, the (2)nd month, or the (3)rd week-day. If we were to put all these example entries together, we would get the following crontab schedule:

5 18 2 2 3

This schedule would run when all these conditions are true — i.e. at 6:05PM on February 2nd, so long as it’s also a Wednesday. An odd schedule to run, but illustrative of how crontab scheduling works. Notice the incredible brevity – Cron’s strongest selling point, alongside its universal availability on Unix-like systems.

More often, the schedules we want to run in real life are quite simple, and involve just one or two conditions. For example, running a task “every hour,” or “every day,” or in our case, “every minute.”

To run a task every hour we would set the schedule to run on the (1)st minute, every hour, every day, and so on. It would look like this:

1 * * * *

Here is the schedule for 3:00AM every day:

0 3 * * *

And here for “every Monday at midnight.”

0 0 * * 1

Our case is even simpler: we want the Mac to announce the time every minute, of every hour, of every day, and so on. Our Crontab schedule is simply five asterisks separated by spaces:

* * * * *

So that’s the scheduling part of the entry we want to put inside our crontab text file. To complete our entry, we simple add the task on at the end as a sixth entry on the same line. A crontab can contain multiple entries, each on their own line, but we only want to schedule one task. So the entire contents of our crontab text file will look like this:

* * * * * uptime >> ~/uptime.log

Remember, the Crontab file is just a plain text file — so all we need to do is create a .txt file with the above contents. You can use your text editor of choice for the task, but it’s easier to demonstrate here doing it on the command-line. I’m going to create a file called “crontab.txt” in my home folder, then add the contents. If you like, you can save the Crontab anywhere, and call it anything you want.

touch ~/crontab.txt
echo '* * * * * uptime >> ~/uptime.log' > ~/crontab.txt

Now we’ve created our Crontab file, all we need to do is point Cron to it so it can import and schedule our task. This is done via a simple command:

crontab ~/crontab.txt

Now run the following task to list the scheduled cron jobs, which should confirm your task is now scheduled:

crontab -l

Log out, restart, change user accounts — the cron job will still be able to run the task we’ve just scheduled.

Don’t have any need for the uptime log we just scheduled? Clear all scheduled cron jobs by running the following:

crontab -r

So that’s a simple Cron job scheduled, then unscheduled. Now with the basics of scheduling Cron jobs under your belt, you should be able to think up some productive uses and put it to real work.

You can schedule local or remote backups of folders using the cp or rsync commands, for example. I have a cron job set up to copy a directory of important files to a remote VPS server every hour. The copy is done via the rsync command, so only changes to the folder are transferred (if there are any). Really, the sky is the limit as to what you can schedule.

Advanced Tips

  • To schedule more complex tasks in a cron job, you can enter the file path to a shell script as the sixth entry in your crontab instead of typing the command in directly. In this case, you will need to keep the shell script in place after the Cron job has been scheduled.
  • As well as entering a simple asterisk or number in the time entries, you can use more complex syntax to get greater customisation. */2 would mean “every two (minutes, hours, months, etc.). 10–12 would mean “every 10th, 11th, and 12th (minute, hour, month, etc.).
  • Each user on your computer has their own space for Cron jobs. When you run crontab -l, only the Cron jobs for the currently logged-in user are listed. Similarly crontab -r only clears the current user’s Cron jobs. These tasks belong to and run as that user at the command-line level — however they execute whether or not that user is currently logged in to the GUI, or has active sessions open.
  • Make sure that all entries in your crontab text file are followed by a newline. In other words, hit return at the end of the line. If this is missing Cron might not pick up the task correctly.
Like what you read? Give Ross Barnes a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.