Crontab timed execution command under Linux

Crontab timed execution command under Linux


table of Contents

  1. Crontab overview and installation
  2. Crontab rules
  3. 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

Insert picture description here

3. If it is not installed, execute the installation command
  1. vixie-cron is the main program of cron;
  2. 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
  1. * : Represents the meaning of "every", for example, if the month field is *, it means that the command is executed every month
  2. , : Indicates the meaning of separating time periods, for example, "1, 3, 5, 7, 9"
  3. - : Indicates a time range, for example, "2-6" means "2, 3, 4, 5, 6".
  4. /: 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
  1. Create a new shell file in the directory: test.sh, write

    #!/bin/bash
    
    echo "hello world!"
    
  2. Give test.sh executable permissions

    chmod 755 test.sh
    
  3. Execute and contab -ewrite timing tasks, and execute the test.sh script every minute.

    */1 * * * * /a8root/home/lijinwang/test/test.sh >> /a8root/home/lijinwang/test/test.log
    
  4. result.
    Insert picture description here


3. Common examples

  1. 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
    
  2. Execute test.sh script every 30 minutes every 6 hours every month

    30 */6 * * * /root/test.sh >> /root/test.log
    
  3. 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
    
  4. Execute the test.sh script at 21:30 every night every month

    30 21 * * * /root/test.sh >> /root/test.log
    
  5. 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
    
  6. 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
    
  7. Execute the test.sh script every hour and hour on the hour every month

    00 */1 * * * /root/test.sh >> /root/test.log
    

Guess you like

Origin blog.csdn.net/weixin_41910694/article/details/108134125