crontab

After entering the Linux system, first check whether the Crontab service is installed

Check if the crontab tool is installed crontab -l

Check if the Crontab service is started service crond status

 

If not installed, install cron

yum install vixie-cron
yum install crontabs

 

Service Operation Instructions:

/sbin/service crond start //Start the service

/sbin/service crond stop //Close the service

/sbin/service crond restart //Restart the service

/sbin/service crond reload //Reload configuration

/sbin/service crond status //Start the service

 

 

Write a timed task, if there is no timed task

Tips are as follows: no crontab for root

Write the task crontab -e (print the current time to the log file log.txt every minute)

*/1 * * * * date >> /tmp/log.txt

Prompt to view the timing task tail -f /tmp/log.txt (continuously display the contents of several lines of the file, dynamic; -3 represents the last 3 lines)

[root@localhost ~]# tail -f /tmp/log.txt
Mon Jul 10 17:19:01 CST 2017
Mon Jul 10 17:20:01 CST 2017
Mon Jul 10 17:21:01 CST 2017
Mon Jul 10 17:22:01 CST 2017
Mon Jul 10 17:23:01 CST 2017
Mon Jul 10 17:24:01 CST 2017
Mon Jul 10 17:25:01 CST 2017
Mon Jul 10 17:26:01 CST 2017
Mon Jul 10 17:27:01 CST 2017
Mon Jul 10 17:28:01 CST 2017
Mon Jul 10 17:29:01 CST 2017
Mon Jul 10 17:30:01 CST 2017
Mon Jul 10 17:31:01 CST 2017

Crontab basic composition

Lei Xiaotian Blog

Format of the Crontab Configuration File

*Represents not specified, that is, it must be executed at all times, that is, every minute, every hour, every day, etc.

Lei Xiaotian Blog

Common cases are as follows

Restart apache every night at 21:30

30 21 * * * service httpd restart 

restarts apache at 4:45 on the 1st, 10, 22nd of every month
45 4 1,10,22 * * service httpd restart

restarts apache at 4:45 on the 1st to the 10th of every month
45 4 1- 10 * * service httpd restart

restarts the apache server every two minutes
*/2 * * * * service httpd restart #even minutes
1-59/2 * * * * service httpd restart #base minutes

between 11pm and 7am , restart apache every hour
0 23-7/1 * * * service httpd restart

every 30 minutes between 18:00 and 23:00 restart apache
0,30 18-23 * * * service httpd restart
0-59 /30 18-23 * * * service httpd restart

runs the test.php file in the root directory every minute
*/1 * * * * /usr/local/php/bin/php /data/wwwroot/door/test.php

 

Summary: 
* means match at any time,
you can use A,B,C to mean or execute a command when A or B or C
can use AB to mean between A and B to execute a command
You can use */A to mean every A minute (hour, etc.) Execute the command
crontab -l to view the scheduled tasks
crontab -e create and edit the scheduled tasks (delete a single one can enter the edit manual deletion)
crontab -r delete all the scheduled tasks
service crond restart restart all the scheduled tasks

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324995972&siteId=291194637