Use of the crontab command for CentOS scheduled tasks
Introduction
Crond is a daemon in linux that is used to periodically execute a task or wait for processing some events. It is similar to a scheduled task in windows. After the operating system is installed, by default, the service tool is installed and the crond process is automatically started. The crond Process regularly checks whether there are tasks to be executed every minute. If there are tasks to be executed, the task is automatically executed.
How to Use
- First view help
[root@dmp004 orderreport]# crontab -husage: crontab [-u user] file crontab [-u user] [ -e | -l | -r ] (default operation is replace, per 1003.2) -e (edit user's crontab) -l (list user's crontab) -r (delete user's crontab) -i (prompt before deleting user's crontab) -s (selinux context)
The usage format is as follows:
Crontab [-u user] [-e |-l |-r] file
-U followed by user name
-E Indicates editing the scheduled task list.
-L list user scheduled tasks
File is the file that defines the scheduled task.
Other parameters are clear at a glance. The file format is very important.
minute hour day month week command
The file contains six fields, the first five fields are time, and the last field is the command to be executed.
Minute indicates the minute, which can be 0-59
Hour indicates the hour, which can be 0-23
Day indicates the day, which can be 1-31
Month indicates month, which can be 1-12
Week indicates the number of weeks, which can be 0-7
Command indicates the command to be executed. It can be a linux command or a shell script.
In the time field:
Asterisk * represents any time
Comma (,). For example, if hour is "3, 4, 9", it indicates that the command must be executed at, and.
Horizontal Bar-indicates that the value range is "3-9", for example, from.
Diagonal lines \/indicate the time interval. For example, if hour is "0-23/2", it indicates that it is executed between and every two hours.
Common
- View scheduled tasks of the current user
[root@dmp004 orderreport]# crontab -l50 17 * * * /home/mofei/task/orderreport/start.sh
- View scheduled tasks under User root
[root@dmp004 orderreport]# crontab -u root -l50 17 * * * /home/mofei/task/orderreport/start.sh
3. Add a scheduled task to the root user
Define test. task as follows:
* * * * * echo 'this is a test'
Add to scheduled task list
[root@dmp004 orderreport]# crontab -u root test.task[root@dmp004 orderreport]# crontab -u root -l* * * * * echo 'this is a test'
- Delete the root scheduled task
[root@dmp004 orderreport]# crontab -u root -r