Administration
...
Background tasks
Configuring Cron for Linux

Crontab basics

7min

Cron is a system scheduler used in Unix-like operating systems. It is used to automatically run scripts, commands, or programs at specified times and dates. A scheduled task is known as a cron job and is defined in a file called a crontab (cron table).

Crontab commands

To edit the crontab file, view cron jobs, display the contents of the current crontab or delete it, enter the following commands in the terminal:

  • crontab -e — edit the crontab. This command will create a new crontab if one does not already exist and will open it in the default text editor.
  • crontab -u username -e — edit another user’s crontab file. To edit other users’ files, you need superuser privileges. Use sudo su before running the command.
  • crontab -l — view cron jobs and display the contents of the current crontab file.
  • crontab -u username -l — view the crontab files list for another user. Run this command with superuser privileges.
  • crontab -r — delete the current crontab file of the current user.
  • crontab -i — this command performs the same function as crontab -r, but prompts for confirmation before deleting the crontab.

Crontab syntax

The crontab file consists of cron jobs (commands). A cron job is divided into two parts: a cron expression followed by the command you want to execute. Here’s an example of a cron job:

Shell


The first five fields specify the time when the cron job will be executed.​ Each field is represented by an asterisk (*) and specifies the timing for the task.

Document image


An asterisk means every possible value for that time unit. Each field can have a single value, a range of values, a list of values, or a special character. The fields are separated by spaces or tabs.

Time unit

Description

Range

Minutes

The minute of the hour when the command will be launched

0-59

Hours

The hour when the command execution will start

0-23

Day of month

The day of the month on which you want to run the command

1-31

Month

The month in which the command will be executed

1-12

Day of week

The day of the week on which you want to execute the command

0-6 (Sunday is 0)

* * * * /bin/sh backup.sh means running a backup every minute.

30 18 * * * rm /home/sydtesting/tmp/* means that files in /home/sydtesting/tmp will be deleted every day at 18:30.

Additionally, it is important to use the special characters.

Character

Description

Example

Asterisk (*)

Represents "every" possible value for the field it is used in

* in the Day of week field means every day of the week

Comma (,)

Used to list multiple values within a single field

5,15,45 in the Minutes field means the job will run at the 5th minute, the 15th minute, and the 45th minute of every hour

Hyphen (-)

Specifies a range of values

1-5 in the Day of week field means the job will run from Monday to Friday

Slash (/)

Indicates increments or step values

*/5 in the Minutes field means the job will run every 5 minutes (0, 5, 10, 15, etc.)

Last (L)

Used in the Day of month and Day of week fields to represent last

0 0 L * * would run the job at midnight on the last day of every month.

Weekday (W)

Specifies the nearest weekday to a given day of the month

15W in the Day of month field means the job will run on the closest weekday to the 15th of the month

Hash (#)

Used in the Day of week field to specify the nth occurrence of a weekday in a month

2#3 means the third Monday of the month

Question mark (?)

Used in the Day of month and Day of week fields as a placeholder for no specific value. It is often used when one of these fields is specified and the other is left as any value

0 0 15 8 ? means "run at midnight on the 15th of August, regardless of the day of the week"

Email notifications

After each cron job is executed, cron automatically sends an email notification to the user. This notification includes the output of the job or any error messages that occurred. If you do not want to receive these emails, just add >/dev/null 2>&1 to the syntax, as shown in the example.

Shell


If you want the output to be sent to a different email address, you can add a MAILTO line at the beginning of the crontab file with the desired email address. For example:

Shell


Cron job examples

Expression

Meaning

0 0 * * * /bin/sh backup.sh

Perform database backup at midnight once a day

0 6,18 * * * /bin/sh backup.sh

Perform DB backup twice a day at 6 am and 6 pm

0 */6 * * * /scripts/monitor.sh

Perfrom monitoring every six hours

*/10 * * * * /home/user/script.sh

Perform cron job for the script file located in the home directory every 10 minutes

0 * 20 7 * /bin/sh backup.sh

Launch database backup every hour every 20th of July

0 0 * * 2 * /bin/sh

Launch DB backup at midnight every Tuesday

* * * 1,2,5 * /script/script.sh

Execute the command in January, February, and May

10-59/5 5 * * * /home/user/script.sh

Execute the command every 5 minutes at 5 am, starting at 5:10 am

0 8 1 */3 * /home/user/script.sh

Launch the command quarterly on the 1st day at 8 am

* * * * * /scripts/script.sh; /scripts/scrit2.sh

Schedule multiple tasks in one cron job

@reboot /scripts/script.sh

Perform a task each time the system is started

0 0 1 * * /home/user/script.sh

Execute the command on the first day of each month