Crontab timed execution command under Linux
table of Contents
- Crontab overview and installation
- Crontab rules
- Common examples
1. Crontab overview and installation
1. Crontab instruction is a command to execute timing tasks under Linux.
2. Check if crontab is installed on the server
rpm -qa | grep crontab
3. If it is not installed, execute the installation command
- vixie-cron is the main program of cron;
- crontabs is a program used to install, uninstall, or list tables used to drive the cron daemon.
yum -y install vixie-cron
yum -y install crontabs
4. The installation takes a long time to start and configure the service
service crond start //启动服务
service crond stop //关闭服务
service crond restart //重启服务
service crond reload //重新载入配置
service crond status //查看crontab服务状态
5. Set up auto start
chkconfig --level 345 crond on
2. Crontab rules
1. crontab file format
* * * * * command
分 时 日 月 周(几) 命令
2. Special character interpretation
*
: Represents the meaning of "every", for example, if the month field is *, it means that the command is executed every month,
: Indicates the meaning of separating time periods, for example, "1, 3, 5, 7, 9"-
: Indicates a time range, for example, "2-6" means "2, 3, 4, 5, 6"./
: Indicates the interval frequency of the time, for example, "0-23/2" means it will be executed every two hours. At the same time/
it can be*
used together, e.g.*
/ 10, if used in the minute field representing every 10 minutes.
3. Practice
-
Create a new shell file in the directory: test.sh, write
#!/bin/bash echo "hello world!"
-
Give test.sh executable permissions
chmod 755 test.sh
-
Execute and
contab -e
write timing tasks, and execute the test.sh script every minute.*/1 * * * * /a8root/home/lijinwang/test/test.sh >> /a8root/home/lijinwang/test/test.log
-
result.
3. Common examples
-
Execute the test.sh script at 3:30 a.m. and 12:20 noon every month
30 3,12 * * * /root/test.sh >> /root/test.log
-
Execute test.sh script every 30 minutes every 6 hours every month
30 */6 * * * /root/test.sh >> /root/test.log
-
Execute the test.sh script every 30 minutes every 2 hours from 8 am to 18 pm every month
30 8-18/2 * * * /root/test.sh >> /root/test.log
-
Execute the test.sh script at 21:30 every night every month
30 21 * * * /root/test.sh >> /root/test.log
-
Execute the test.sh script at 4:45 am on the 1st, 10th, and 22nd of each month
45 4 1,10,22 * * /root/test.sh >> /root/test.log
-
Execute the test.sh script at 1:10 am on Monday and Sunday in August
10 1 * 8 6,0 /root/test.sh >> /root/test.log
-
Execute the test.sh script every hour and hour on the hour every month
00 */1 * * * /root/test.sh >> /root/test.log