10 useful cron examples to schedule jobs in Linux

The GNU/Linux system supports several utilities for scheduling tasks. The cron utility is the most widely supported. It allows you to schedule tasks to be run in the background at regular intervals. The cron utility uses a table (crontab) with a list of scripts or commands to be executed and the time when they are to be executed.

Scheduling jobs

Normal users can use the crontab command to manage their jobs. This command can be called in four different ways:

Command Function
crontab -l List the jobs for the current user
crontab -r Remove all jobs for the current users.
crontab -e Edit jobs for the current user.
crontab [filename] Remove all jobs, and replace with the jobs read from [filename]. If no file is specified, stdin will be used.

How to edit and view crontab

When editing jobs with the “crontab -e“, an editor will be started (vi by default. unless the EDITOR environment variable has been set to something different). The file being edited will have one job per line. Empty lines are allowed, and comments start their line with a hash symbol (#).

# crontab -e

When you execute the command “crontab -e” without any options, the crontab of the currently logged in user will be edited by default. To edit the crontab of another user, you can use the -u option with crontab command. For example, to edit the crontab of the user john.

# crontab -u john -e

Similarly, to view the crontab of the current user, use the “crontab -l” command.

# crontab -l

And to list the crontab of a specific user, use the below command.

# crontab -u john -l

Cron configuration files

The cron background process is mostly idle. It wakes up once every minute and checks /etc/crontab, /etc/cron.d, and the user cron table files and determines whether there are any jobs that need to be executed.

The table below summarizes the purpose of the various files and directories used by cron. Knowledge of these files and directories will help you troubleshoot any issues as well as understand cron in more detail.

File Purpose
/etc/init.d/crond Starts the cron daemon in system boot.
/var/log/cron System messages related to the cron process. Useful for troubleshooting problems.
/var/spool/cron/[username] User crontab files are stored in the /var/spool/crondirectory.
/etc/cron.allow Specifies users who can create a cron table.
/etc/cron.deny Specifies users who are not allowed to create a cron table.
/etc/crontab The system cron table that has commands to run scripts located in the following directories: /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly.
/etc/cron.d A directory that contains cron tables for jobs that need to run on a schedule other than hourly, daily, weekly, or monthly.
/etc/cron.hourly Directory that contains system scripts to run on an hourly basis.
/etc/cron.daily Directory that contains system scripts to run on a daily basis.
/etc/cron.weekly Directory that contains system scripts to run on a weekly basis.
/etc/cron.monthly Directory that contains system scripts to run on a monthly basis.

Crontab syntax

The jobs of individual users are stored in the directory /var/spool/cron in files matching the usernames. These files always belong to the user root. The files in /var/spool/cron are not edited directly. Instead, a program called crontab is used to manipulate them. The figure below shows the syntax of a cron job.

Each line in a file defines a job. There are 6 fields in a line. The first 5 fields define the time, the final field contains the command to run. This can be any type of command or shell script. The first 5 fields have the following format:

Field Range
Minutes 0-59
Hours 0-23
Day of the Month 1-31
Month 0-12
Weekday 0-7

Example cron jobs

1. Schedule a job at February 2nd, every year

You need to run a backup script once every year on a specific date in february month. The sytax is shown below.

0 9 2 2 *   /usr/local/bin/yearly_backup

2. Schedule a job every hour at the fifth minute, every day

Use the following command to run a script every hour at the fifth minute, every day:

5 * * * *      $HOME/bin/daily.job >> $HOME/tmp/out  2>&1

3. Schedule a job 5 minutes after midnight every day

Use the following command to run 5 minutes after midnight every day:

5 0 * * *      $HOME/bin/daily.job >> $HOME/tmp/out  2>&1

4. Schedule a job at a specific time on on the first of every month

Use the following command to run at 2.15 P.M. on the first of every month:

15 14 1 * * *     $HOME/bin/monthly

5. Schedule a job at a specific time only on weekdays (exclude Saturday and Sunday)

Use the following command to run at 10 P.M. on weekdays:

0 22 * * 1-5  $HOME/bin/weekday.job >> $HOME/tmp/out  2>&1

6. Scheduling multiple jobs in a single cron job

We can also schedule multiple jobs in a single cron job using a semicolon(;) separated jobs as showb below:

0 12 * * * /var/tmp/script01.sh; /var/tmp/script02.sh

7. Using ranges to specify starting times

Ranges can be specified through the use of a hyphen. A value of 1-5 indicates that this field is valid for numbers 1 through 5. If you use a name instead of a number, you cannot specify a range. Example of specifying ranges is shown below. The Job is scheduled to run every hour from 3 P.M. To 10 P.M.

* 3-10 * * * /var/tmp/script.sh

8. Using step values to specify starting times

Step values can be used in conjunction with ranges. To specify a step value, follow the range with a forward slash (/) and a number. The number specified is the step value. For example, the following specifies that every third value (in this case, 2, 5, 8, and 11) should be matched:

* 0-12/3 * * *  /var/tmp/script.sh

Step values can also be used with asterisks. The value */3 in the hour field would match every third hour (0, 3, 6, 9, 12, 15, 18, and 21).

9. Using lists to specify starting times

Lists are also acceptable; each item in a list is separated by a comma. It is common to use lists in conjunction with ranges, like so:

1-15,31-45 * * * *  /var/tmp/script.sh

This example matches all numbers(minutes in our case) from 1 through 15 and from 31 through 45. If you use a name instead of a number, you cannot specify a list.

10. Schedule a job at 4:45 a.m., on the 1st, 10th and 22nd of each month

Shown below is an example of running a job at a specific time on few specific days of the month.

45 4 1,10,22 * * /apps/bin/backup.sh 

Crontab Macros

We can add macros in the crontab file. For example, use the following to restart “my_script” after each reboot:

@reboot  /var/tmp/my_script

The following is a summary of a few more macros:

Entry Description Equivalent To
@reboot Run once at start-up None
@weekly Run once a week 0 0 * * 0
@daily Run once a day 0 0 * * *
@midnight (same as @daily) 0 0 * * *
@hourly Run once an hour 0 * * * *

Dealing with Output

A script often prints something to the screen, either for debugging, status updates, or to log an error. Anything that a job prints to the screen is sent in an email to the current user, which can be overridden with the MAILTO variable inside the crontab.

# crontab -l
MAIL=john
0 2 * * * /apps/bin/backup.sh
Related Post