Cron is a daemon that executes scheduled commands for Linux. You can use Cron to schedule and automate jobs in Linux, saving you from having to do them manually.
Note: Cron is available in most *NIX system, including Mac OS X. In this article, we will be discussing from the Linux perspective.
How does cron work?
Put simply, cron scans your computer for text files and executes the commands inside them at regular intervals. Cron does not execute just any text files, only the ones located in a specific location on your hard drive. These text files are called “crontabs”.
A crontab is a text file with a list of commands that will automatically run at specific times. These commands are referred to as cron jobs. The cron daemon executes the cron jobs within the crontab and handles the scheduling, all in the background.
There is a root crontab for tasks that require administrative privileges. Each user also has their own crontab file so that they each may run their own personal set of tasks, and these tasks will run regardless of whether or not that user is logged in.
Cron searches the area located at /var/spool/cron/. This location is where crontabs are stored. If I open this file using a general text editor, this is what is displayed.
Note the warning not to edit this file manually. You can edit it directly inside the terminal, but there are also specific applications for editing crontabs. GNOME users may be interested in checking out Gnome Schedule. KDE users can check out the KDE Task Scheduler.
Viewing a crontab
If you want to manually view your crontabs, here are some basic commands.
To display a crontab:
To remove a crontab:
To edit a crontab:
When you edit a crontab within a terminal for the first time, cron will ask you to select an editor. It points out which editor it believes to be the easiest, which is probably nano. To change this in the future, type:
It then opens a blank crontab from the templates directory. Anything saved in this file will be executed as the user.
Reading a cron job
A cron job can be broken down as follows:
Minute: 0 – 59
Hour: 0 – 23 (0 = midnight)
Day: 1 – 31
Month: 1 – 12
Weekday: 0 – 6 (0 = Sunday)
Command: Command to execute
A * means to execute at every interval.
Take a look at this command:
In the above example, I am running /usr/bin/sbin/backup.sh every Saturday at 7:04 AM regardless of the month or date.
The image below provides an sample cron job and a look at the appropriate syntax:
Even if you have never opened up a crontab before, your computer likely has cron jobs already operating in the background. Cron makes it easy for a distribution to check for updates at regularly scheduled intervals or to know when to back up files. Regardless of which application you use to create and edit future cron jobs, the syntax inside a crontab remains the same.
With these tools at your disposal, you can automate tasks with cron and can set your music to begin playing at specific times of the day, have your email refreshed before you sit down at your computer each morning, tell your computer to shut down at a specific time every night, or nearly any other single task you are accustomed to doing by hand.