Under Centos 7 crontab is used to perform task details regularly

  • 2020-06-07 05:56:31
  • OfStack

preface

The cron service is a built-in service of Linux, but it does not start automatically on boot. You can start and stop the service with the following commands:


/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart
/sbin/service crond reload

Lines 1-4 above are starting, stopping, restarting the service, and reloading the configuration, respectively.

To set cron to the boot automatically start when, in the/etc/rc d/rc local scripts to join/sbin service crond start can

To view the current user's crontab, type crontab -l ;

Edit crontab, enter crontab -e ;

Delete crontab and type crontab -r

Add tasks


 crontab -e
 0 */1 * * * command
 0 */2 * * * command

Query if the task has been added:


 crontab -l -u root # To view root The user 
 0 */1 * * * command
 0 */2 * * * command

Basic format:

* * * * * command

Time division day month week command

Column 1 represents minutes 1 to 59 per minute represented by * or */1

Column 2 represents hours 1 to 23 (0 represents 0 points)

Column 3 represents dates 1 through 31

Column 4 represents the months from 1 to 12

Column 5 Identifies Week 0 ~ 6 (0 represents Sunday)

Column 6 is the command to run

Some examples of crontab files:


30 21 * * * /usr/local/etc/rc.d/lighttpd restart

The example above represents the nightly restart of apache at 21:30.


45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart

The above example shows the apache restart at 4:45 on the 1st, 10th, 22nd of each month.


10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart

The above example shows the restart of apache on Sunday at 1:10 on the 6th of every week.


0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart

The above example shows that apache is restarted every 30 minutes between 18:00 and 23:00 each day.


0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart

The above example shows apache restarting at 11:00 pm every 6 weeks.


* */1 * * * /usr/local/etc/rc.d/lighttpd restart

Restart apache every hour


* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart

Restart apache every hour between 11 p.m. and 7 a.m


 crontab -e
 0 */1 * * * command
 0 */2 * * * command
0

apache restarts on the 4th of every month and at 11:00 from the 1st to the 3rd of every week


 crontab -e
 0 */1 * * * command
 0 */2 * * * command
1

apache will restart at 4:00 on January 1

conclusion


Related articles: