Crontab scheduled task, crontab task
1. crontab Introduction
Crontab commands are common in Unix and Linux operating systems and are used to set periodically executed commands. This command reads commands from the standard input device and stores them in the crontab file for later reading and execution. Generally, commands stored in crontab are activated by the daemon. Crond is often run in the background and checks every minute Whether a scheduled job needs to be executed. This type of job is generally called cron jobs.
All user-defined crontab files are saved in the/var/spool/cron directory, and their names are consistent with the user name.
Use the following control file:
Blacklist:/etc/cron. deny: the crontab command is not allowed for users listed in this file.
Whitelist:/etc/cron. allow users listed in this file can use the crontab command
These two files are not used by default in Ubuntu. By default, all users can use the crontab command. The format of the two control files is one user per line, and spaces are not allowed for both files. If the control file is modified, the cron daemon (crond) does not have to be restarted. The control file is read every time you add or delete a cron task. Regardless of the rules in the control file, root can always use cron. If the cron. allow file exists, only the listed users are allowed to use cron, And the cron. deny file is ignored. If the cron. allow file does not exist, all users listed in cron. deny are forbidden to use cron.
If you create a new scheduling task, enter the crontab-e command to create your crontab file, or add it to the/etc/crontab file. Pay attention to the editing permission.
2. crontab task format
In the crontab file, each row represents a task, and each field in each row represents a setting. Its format is divided into six fields, and the first five fields are time sets, the sixth part is the command segment to be executed. The format is as follows:
Minute hour day month week command
Where:
Minute: minute. It can be any integer from 0 to 59.
Hour: indicates the hour, which can be any integer from 0 to 23.
Day: indicates the date, which can be any integer from 1 to 31.
Month: represents the month, which can be any integer from 1 to 12.
Week: the day of the week. It can be any integer from 0 to 7. Here 0 or 7 represents Sunday.
Command: the command to be executed. It can be a system command or a script file compiled by yourself.
You can also use the following special characters in the preceding fields:
Asterisk (*): represents all possible values. For example, if the month field is an asterisk, this command is executed every month after the conditions of other fields are met.
Comma (,): values separated by commas (,) can be used to specify a list range, for example"
Middle bars (-): You can use the middle bars between Integers to represent an integer range. For example, "2-6" indicates "2, 3, 4, 5, 6"
Forward slash (/): You can use a forward slash to specify the interval of time. For example, "0-23/2" indicates that execution is performed every two hours. At the same time, the forward slash can be used with the star number, for example, */10. If it is used in the minute field, it indicates that the execution is performed every ten minutes.
3. cron Service
The cron service is installed in Ubuntu by default. CentOS requires the yum install crontabs manual installation and the service name is crond.
Service operation instructions:
Service cron start // start the service
Service cron stop // close the service
Service cron restart // restart the service
Service cron reload // reload the configuration
Service cron status // view service status
View the cron process and execute the command: ps aux | grep cron
4. crontab command details
1) Command Format:
Crontab [-u user] file
Crontab [-u user] [-e |-l |-r]
2) command functions:
Through the crontab command, we can execute the specified system command or shell script at a fixed interval. The time interval can be any combination of minutes, hours, days, months, weeks, and above. This command includes non-permanent and periodic log analysis or data backup.
3) command parameters:
-U user: used to set the crontab service for a user. For example, "-u ixdba" indicates to set the crontab service for the ixdba user. This parameter is generally run by the root user.
File: file is the name of the command file. It indicates that file is used as the task list file of crontab and is loaded into crontab. If this file is not specified in the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into the crontab.
-E: edit the contents of a user's crontab file. If no user is specified, the crontab file of the current user is edited.
-L: displays the contents of a user's crontab file. If no user is specified, the contents of the current user's crontab file are displayed.
-R: deletes the crontab file of a user from the/var/spool/cron directory. If no user is specified, the crontab file of the current user is deleted by default.
-I: A confirmation prompt is displayed when the user's crontab file is deleted.
4) application example:
① Edit the content of the crontab file:Crontab-e
When you use this command for the first time, you will be asked to select the text editor, usually vim. You can also use select-editor to modify the EDITOR environment variables, for example, EDITOR = vi.
For the edited content format, see"2. crontab task format", Save the modification and restart the service to execute the new task, sudo service cron restart.
② List the contents of the crontab file:Crontab-l
Usually used to back up crontab, such as: crontab-l>/home/mycrontab. bak
③ Delete the crontab file:Crontab-r
5. crontab format instance
Instance 1: Execute command once every minute
Command: *** command
Instance 2: 3rd and 15th minutes per hour
Command: 3, 15 *** command
Instance 3: Run at 3rd and 15th minutes from eight o'clock A.M.
Command: 3, 15 8-11 *** command
Instance 4: runs every two days, from eight o'clock A.M. to, at 3rd and 15th minutes.
Command: 3, 15 8-11 */2 ** command
Instance 5: run the task at 3rd and 15th minutes from eight o'clock A.M. to every Monday.
Command: 3,15 8-11 *** 1 command
Instance 6: restart smb at every night
Command: 30 21 ***/etc/init. d/smb restart
Instance 7: restart smb at on the 1st, 10th, and 22th of every month
Command: 45 4, 10, 22 **/etc/init. d/smb restart
Instance 8: restart smb every Saturday or Sunday
Command: 10 1 ** 6, 0/etc/init. d/smb restart
Instance 9: restart smb every 30 minutes between and every day
Command: 0, 30 18-23 ***/etc/init. d/smb restart
Instance 10: restart smb at every Saturday.
Command: 0 23 ** 6/etc/init. d/smb restart
Instance 11: restart smb every hour
Command: **/1 ***/etc/init. d/smb restart
Instance 12: restart smb every hour between PM and PM.
Command:
* 23-7/1 ***/etc/init. d/smb restart
Instance 13: restart smb at from Monday to Wednesday on the 4th of each month
Command: 0 11 4 * mon-wed/etc/init. d/smb restart
Instance 14: restart smb at on January 1, January 1
Command: 0 4 1 jan */etc/init. d/smb restart
Example 15: execute scripts in the/etc/cron. hourly directory every hour.
Command: 01 *** root run-parts/etc/cron. hourly/
Note: If the run-parts parameter is removed, you can write a script name to run later, instead of the directory name.