The Cron Daemon

Scheduling Recurring Tasks on Linux Using Cron

Cron is a daemon used to schedule any kind of task you can imagine.  It is useful to send out emails on system or program statistics, do regular system maintenance, make backups, or do any task you can think of.  There are similar programs on other Operating Systems.  On Mac OS X, cron has been replaced with another daemon called launchd.  On Windows you have the aptly named “Task Scheduler”.  If you are craving a GUI for Linux, Gnome-based systems like Ubuntu, include Gnome Schedule which acts as a nice front end for cron.

The Cron daemon, Crond

The cron daemon uses crontab files to specify what tasks to run and when.  These are text files that specify command to run at certain time intervals.  Each individual user can have their own crontab file.  There are also system wide crontab files, used to collaborate or share among users.  The cron daemon will examine user crontab files stored in var/spool/cron, and system wide crontab files in /etc/crontab & /etc/cron.d It wakes up every minute to do this.

Crontab

Crontab is the command used to list the tables used by the cron daemon.  We also call the files used to load the cron daemon, crontab files.  When we use the crontab –e command, we are editing the crontabs in /var/spool.  The files locates here are in a different format and should never be edited directly.

Editing Crontab Files aka Creating Cron Jobs

Let’s get started by editing our own user crontab file:

klack@:~$ crontab -e

no crontab for klack - using an empty one<
 Select an editor.  To change later, run 'select-editor'.
1. /bin/ed<
2. /bin/nano        <---- easiest
4. /usr/bin/vim.tiny

Choose 1-4 [2]: 2

The first time we try to edit our crontab file we will be asked to choose an editor.  Nano is marked as the easiest editor.  Go ahead and choose nano by entering 2.  Crontab will now create a temporary copy of your crontab file and open it in nano for editing.

When nano starts up, press Ctrl-V repeatedly to scroll down to the bottom of the file.  You should now be on a new line ready to enter our first cron command.

Crontab Format

Each line in the crontab file can be a variable definition or cron command.  Don’t forget to terminate the last command with a newline character, by pressing the enter key.  You can also end the last statement with a %. This is the most common error that will cause your command to fail, in this case an error will not be displayed.  Cron commands have a distinct pattern that allows you to specify custom recurring times.  There are five fields used specify the time and the command to run.

(Minute 0-59) (Hour 0-23) (Day of Month 1-31) (Month 1-12) (Day of week 0-7) (Command)

Crontab Examples

Here are several examples that will run a backup script at different time intervals:

0 5 1 * * ~/backup-home.sh

What we have specified here is to run this backup-home.sh script at 5:00 AM every 1st of the month.  The * signifies a wild card.  In this example the task is run on all months and any day of the week.

 

0 5 1,15 * * ~/backup-home.sh

In this modified example we used a comma in the day of the month field to signify that we want this task run on the 1st and 15th of each month.

 

0 5 1 12/3 * ~/backup-home.sh

In this example 12/3 represents a step value.  This means the command will be run every quarter on the 1st of the month at 5 AM.

 

* 17-20 * * * ~/backup-home.sh

Here this backup script would run at hours 17,18,19 and 20 every day.

 

* * * * * ~/backup-home.sh

This example would run every single minute.  Be careful if you think you have to  use this one!

 

*/2 * * * * ~/backup-home.sh

Combining some ideas here, this command includes a step value and would run every two minutes.

 

@reboot ~/backup-home.sh

Here we are using a special time signification @reboot will run once after every reboot.  There are other special time keywords that you can use.  Here is a full list:

@reboot     :    Run once after reboot.
@yearly     :    Run once a year
@annually   :    Run once a year
@monthly    :    Run once a month
@weekly     :    Run once a week
@daily      :    Run once a day
@hourly     :    Run once an hour

When you are done entering your scheduled commands save your file in nano by pressing Ctrl-W and enter.  Then exit by pressing Ctrl-X.  Crontab will now check the file for errors.  If no errors are found then the tables used by cron daemon will be updated and the changes will take immediate effect.

Cron.hourly Cron.daily Cron.weekly Cron.monthly

For easy no fuss use of cron jobs, just create a file in one of these directories located in /etc/  The task will be run according to folder it is placed in.  Doing it this way give you less control over the time however (When during the week?  Sometime, for sure).

cron.Allow and cron.Deny

These files are possibly located in /etc they control who can use the crontab command.  If cron.allow exists they only the uses listed there (one user per line), can run crontab.  If cron.allow does not exist, then any user can have crontab files, except for those listed in cron.deny.  If neither of these files exist, then superuser privileges are required to have user crontab files.

System Wide Crontab Files

In a larger user environment, it might be more beneficial to edit the files at /etc/crontab or /etc/cron.d/.  These files are also loaded by the cron daemon.  In a larger user environment users that collaborate might want to store and see all the crontab files in a central location.  /etc/crontab is a single file, and it includes an extra field, the user field.

0 5 1 * * root ~/backup-home.sh

This system crontab entry would run as root.

Files of any name can be created in the /etc/cron.d/ directory.  Many packages use this directory as it provides for better organization.

Wrapping It All Up

By now you should be able to run any task you can think of with the combination of scripts and the cron daemon (also called cron jobs).  Feel free to leave any questions in the comments below.  Linux Academy members can view instructor led video here.

Hands-On Linux Training