SoFunction
Updated on 2025-03-10

Detailed explanation of using crontab to create timing tasks under Linux

Crontab can be used to create timing tasks under Linux.

Commonly used matching

crontab -e Edit tasks

crontab -l View all tasks [this user]

crontab -r Cancel all tasks [this user]

Task format

× × × × × +Command (specific tasks)

The first 5 parameters represent time, in turn:

Parameters Range

Minutes 0-59

Hours 0-23

Date 1-31

Month 1-12

Week 0-6 (0 represents Sunday)

Special symbols

In order to accurately represent timing, some special symbols are needed to describe the specific task execution time. There are several symbols:

"/" represents every time, how long does it take to go

"*" represents any time

"-" represents a period of time from one moment to another

"," represents discrete time

Case

0 8  1 1 0command

Command is executed at 8:00 on January 1st every year

*/30 *  * * *command

Perform command every 30 minutes

0 6-18  * * *command

Command is executed every day from 6:00 to 18:00

30 6,9,12  * * *command

Command is executed every day at 6:30, 9:30, and 12:30

0 6-18/3  * * */user/bin/PHP

/var/www/html/Execute every 3 hours from 6:00 to 18:00 every 3 hours using /user/bin/php /var/www/html/

Notice

The commands set in crontab are all relative paths, so the scripts executed in the command should be set to an absolute path as much as possible. On the other hand, if there is output in the script, it will be output to the current user directory. Therefore, if there is a path in the script, please set it to an absolute path!

Crontab errors will be sent to the user by email. The directory under centos is /var/spool/mail/user. Long-term accumulation may cause the space under /var/ to take up too much space, so it should be cleaned up in time. (dG in vi)

The above is a detailed explanation introduced by the editor to create timed tasks using crontab under Linux. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!