Crontab command explanation

The crontab command is used to submit and manage user tasks that need to be executed periodically. It is similar to the scheduled tasks in Windows. After the operating system is installed, this service tool will be installed by default, and the crond process will be automatically started. The crond process will regularly check every minute whether there are tasks to be executed. If there are tasks to be executed, the task will be automatically executed.

Here’s a more detailed introduction:

crontab

The crontab command is used to edit a user's crontab file. A crontab file is a text file that contains a list of commands to be executed at specific times.

Command syntax

The crontab command has the following syntax:

crontab [options]

Options

The following options are available for the crontab command:

-e : Edit the crontab file.

-l : List the crontab file.

-r : Remove the crontab file.

-u user : Edit the crontab file for the specified user.

Crontab file format

A crontab file consists of a series of lines, each of which specifies a command to be executed. Each line is divided into five fields, which are separated by spaces or tabs:

Minute : The minute at which the command should be executed.

Hour : The hour at which the command should be executed.

Day of month : The day of the month at which the command should be executed.

Month : The month at which the command should be executed.

Day of week : The day of the week at which the command should be executed.

Examples

The following example shows how to add a command to the crontab file:

crontab -e

This will open the crontab file in a text editor. Add the following line to the file:

0 0 * * * /path/to/command

This will cause the command /path/to/command to be executed at midnight every day.

The following example shows how to list the crontab file:

crontab -l

This will list the contents of the crontab file.

The following example shows how to remove the crontab file:

crontab -r

This will remove the crontab file.

More crontab examples