Learn How to Schedule Tasks/Jobs with Cron (crontab) in Linux

Learn How to Schedule Tasks/Jobs with Cron (crontab) in Linux

  • Linux
  • 5 mins read

In this tutorial, you will learn how to schedule tasks/jobs with Cron (crontab) in Linux.

The Cron Daemon

The cron daemon is the system job that runs scripted jobs on a pre-determined schedule. The crontab command is used to tell the cron daemon what tasks the user wants to run and when to run those jobs.

Each user on the system may have their crontab file unless the system administrator has restricted them. The crontab file is only manipulated using the crontab command and should never be edited directly.

crontab Command Options in Linux

The crontab command allows us to view, edit, or replace the current user's crontab file. If you receive an error like the following when you run the crontab command, you will need to ask your system administrator to allow you to set up a crontab.

$ crontab -e
crontab: you are not authorized to use cron. Sorry.

Below table shows the common options for the crontab command. Notice that only the root user can change other user's crontabs.

Crontab Options

OptionPurpose
-eEdit the current crontab file using the text editor specified by the EDITOR environment variable or the VISUAL environment variable.
-lList the current crontab file.
-rRemove the current crontab file.
-uSpecifies the user's crontab to be manipulated. This is usually used by root to manipulate the crontab of other users or can be used by you to identify correctly crontab to be managed. If you have used the su command to assume another identity.

For example, to list the current user's crontab, you can use the -l option for the crontab command.

List the Tasks of crontab

$ crontab -l

By using the -e option for the crontab command, we can edit the current user's crontab. This will bring a copy of the crontab up in your default editor. You can then edit the crontab, and when you exit the editor the crontab command will put your changes in place.

crontab also accepts a file name and will use the specified file to create the crontab file. Many users prefer to use this option rather than the crontab -e command because it provides a master file from which the crontab is built. Thus providing a backup to the crontab.

The following example specifies a file called mycron.tab to be used as the input for crontab.

$ crontab mycron.tab

Remove All Jobs from crontab

To remove the current user's crontab, you can use the -r option. The following is an example:

$ crontab -r

crontab File Format

The crontab file consists of a series of entries specifying what shell scripts to run and when to run them. It is also possible to document crontab entries with comments.

Lines that have a pound sign (#) as the first non-blank character are considered comments. Blank lines are entirely ignored. Comments cannot be specified on the same line as cron command lines but must be kept on their lines within the crontab.

crontab Entries

Each crontab entry line is comprised of six positional fields specifying the time, date, and shell script or command to be run. The format of a crontab entry is described in the table below:

FieldMinuteHourDay of MonthMonthDay of WeekCommand
Valid Values0-590-231-311-120-6Command path/command

Each of these fields can include a single number, a range of numbers shown with a hyphen (such as 2-4), a list of specific values parted by commas like 2, 3, 4, or a combination of these classifications separated by commas, such as 1,3,5.

Any of these fields may also contain an asterisk (*) indicating every possible value of this field. This can all get somewhat confusing, so let's take a look at a few examples.

Schedule Tasks/Jobs with Cron (crontab) Examples in Linux

# Daily full report
00 01 * * * /u01/app/oracle/admin/oss/scripts/full_export.sh

This entry will run the full_export.sh script at 0 minutes past 1 am, every day of the month, every month of the year, and every day of the week. As with our shell scripts, comments help make the crontab file more readable.

# Weekly full hot backup
00 03 * * 0 /u01/app/oracle/admin/common/scripts/hot_backup.sh

This entry runs at 3:00 am, but only on Sundays (day 0). While the day of month and month fields have asterisks, this entry will only be run on Sunday since crontab entries will only be executed when all the given values are met.

# Nightly incremental hot backup
00 03 * * 1-6 /u01/app/oracle/admin/common/scripts/hot_backup.sh

In this entry we have specified that the script should be run at 3:00 am Monday through Saturday.

00,15,30,45 * * * * /u01/app/oracle/admin/common/scritps/check_disk_space.sh

This entry has minutes separated by a comma indicating that it should be run at each of the indicated times. Since all the other fields are wildcards (*) the entry will be run on the hour (00), 15 minutes past the hour, 30 minutes past the hour and 45 minutes past the hours for every hour of the day and every day of the year.

See also: