Overview
The Linux system is serviced by the cron (Crond) system to control routine scheduled tasks. Linux systems have a lot of planned work on them, so this system service is started by default.
In addition, because the user can set up scheduled tasks themselves, the Linux system also provides a command for the user to control scheduled tasks: the crontab command.
The task scheduling under Linux is divided into two categories, system task scheduling and user task scheduling.
System task scheduling: The work to be performed by the system periodically, such as writing cache data to hard disk, log cleanup, etc. In the/etc directory there is a crontab file, this is the System Task Scheduler configuration file.
User Permissions File:
File:
/etc/cron.deny Description: The user listed in this file is not allowed to use the crontab command
File:
/etc/cron.allow Description: Users listed in this file are allowed to use the crontab command
File:
/var/spool/cron/Description: The directory where all user crontab files are stored, named after the user name
What the crontab file means:
In the crontab file created by the user, each line represents a task, each field of each row represents a setting, its format is divided into six fields, the first five is the time setting segment, and the sixth paragraph is the command segment to execute, in the following format:
Minute hour day Month Week command
In each of these fields, you can also use the following special characters:
Asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.
Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"
Forward slash (/): You can specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.
Use of CRONTAB commands under Ubuntu
1. First edit the crontab file.
Crontab-e #打开你的用户所属的crontab文件. The first time you use this command, you will be asked to select a text editor
You can change the editor later by command
Select-editor
The open crontab file looks like this:
- # m H Dom Mon Dow command
- */2 * * * *date >> ~/time.log
The second line is a recurring task I wrote for the test, which means that the date >> ~/time.log command is executed every two minutes (recording the current time to the Time.log file). You can add it to your crontab and then save the exit.
After saving the crontab, we also need to restart Cron to apply this scheduled task. Use the following command:
- sudo service cron restart
Implementation of scheduled tasks under Ubuntu