Scheduling Magic: Intro to Cron on Linux

3453

You might not be aware of this, but magic happens in the background of the Linux operating system. Without your help or intervention, programs start and daemons run. These things happen because Linux has an outstanding scheduling system known as cron. Want to make some magic? Let’s get to know cron.

The cron utility allows the user to manage scheduled tasks from the command line. Once a user understands how cron works, it’s not difficult to use. But for some, the understanding can be a challenge. Users must understand how Linux interprets and reads time on a system. Users also need to know how to edit their crontab files. Once a user has a full understand of these concepts, they will be masters of cron. Let’s examine cron and how to create proper entries in a users’ crontab file.

By default, a version of cron (there’s more than one implementation) will be already installed on the Linux system, so there is no need to worry about installation the tool. And as for its use, there are two commands associated with cron:

  • cron: The daemon that is used to execute scheduled commands.
  • crontab: The command used to invoke the editor for managing a users cron jobs.

A users’ crontab file is the file that holds the jobs read by cron. Each user on a system can have a crontab file (this includes the root user) where jobs and tasks can be controlled. The system itself also has a crontab file located at /etc/crontab, but should not be edited by the user. This file is generated upon the installation of the operating system. If the /etc/crontab file is examine it is revealed that it actually controls cron jobs that are located within /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly. But that file isn’t going to be the focus here. Instead the user crontab file will be the primary focus, as that is the file used for the scheduling of ordinary user tasks.

Time

The one aspect of cron that trips most users up is the way in which time is used. For each crontab entry a specific time is declared for when the entry will run. The time entry is in the form:

0 23 * * *

Each time entry consists of five sections:

  • Minute (0-59)
  • Hour (0-23 With 0 being 12:00 AM)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-6 With 0 being Sunday)

So a typical entry would look like:

Minute Hour Day Month DayOfWeek

Some examples for time:

0 23 * * * Daily at 11 PM

30 22 * * * Daily at 10:30 PM

0 23 1 * * Every first day of the month at 11 PM

0 23 * * 0 Every Sunday at 11PM

Using the crontab Utility

Now that time is understood, it’s time to begin adding entries. In order to view a users’ crontab file the crontab command is invoked. There are three main options to use with the crontab command:

  • e: Edit the crontab file.
  • l: List the contents of the crontab file.
  • r: Remove the contents of the crontab file.

When the command crontab -l is invoked the entries for the users’ crontab file will be displayed (if any exist). In order to add an entry to a users’ crontab file, the command crontab -e is invoked so the crontab file will be opened in the default editor (such as ed, vim.tiny, or nano). When the crontab -e command is run for the first time, the default editor is set. To select the default editor for crontab, select the number which corresponds to the editor desired.

Figure 1 shows a crontab entry created by the Luckybackup backup application.

Figure 1A crontab open, with Nano as the default editor, showing the Luckybackup entry.

To illustrate how to add a new entry into crontab, a simple backup script will be used. The contents of that script might look like:

#! /bin/bash
echo Backup Started `date` >> ~/backuplog
mkdir /media/EXT_DRIVE/backups/`date +%Y%m%d`
tar -czf /media/EXT_DRIVE/backups/`date +%Y%m%d`/data.tar.gz /data
echo Backup Completed `date` >> ~/backuplog

Where EXT_DRIVE is the location of an externally attached drive where the backup data will reside.

The above script will be saved in the users’ home directory as .my_backup.sh and given executable permission with the command chmod u+x ~/.my_backup.sh. Now, with crontab in edit mode, create an entry that will execute the script every night at 11 PM, add the following line:

* 23 * * *   ~/.my_backup.sh

With that entry in place, save and close the editor (how this is done will depend upon the default editor you have chosen). When this is done, as long as there are no errors, crontab will report “crontab: installing new crontab” to indicate the entry was successful. If there are errors, open the crontab file back up to make the necessary changes.

Editing the crontab of a Different User

Say a different users’ crontab must be edited. It is not necessary to su to that different user, as crontab has an option built in for that specific purpose. If crontab is issued using the -u like crontab -e -u USERNAME, the crontab file of the user specified (Where USERNAME is the user in question) will be opened for editing. This command, however, can only be issued by a user with administrative user (or the command can be issued using sudo.) Of course, editing other users’ crontab files should be limited only to administrators.

Final Thoughts

The cron system helps to make Linux one of the most flexible operating systems around. Cron not only helps the system keep its logs rotated and clean, it allows users to schedule their own tasks, scripts, and jobs. Although the time aspect of cron can be a bit tricky to grasp, once it is understood, the rest falls into place.

If the whole idea of editing cron entries from the command line seems a bit much, you will be glad to know there are GUI tools for this task. Take a look at a tool like GNOME Schedule (found in your Add/Remove Software tool) for an application that can manage your cron tasks with the help of a user-friendly GUI. But for those who really want to understand Linux, getting to know cron and crontab is essential.