Last update at :2024-03-05,Edit by888u
When we use a VPS on a Linux system, we sometimes need to use scheduled tasks, such as backing up data every day, restarting the MySQL service once a month, etc. These scheduled tasks can be easily implemented with the help of crontab. This article introduces the Linux crontab command and how to use conrtab to set up scheduled tasks for scheduled execution every minute/hour/daily/monthly.
Introduction to crontab
The crontab command is common in Unix and Unix-like operating systems and is used to set instructions to be executed periodically. In Linux, crontab is also a very commonly used command to execute programs regularly.
How to use crontab
Basically what we commonly use is editing crontab commands and viewing crontab commands. The usage is as follows:
# View all current crontabs of the user crontab -l #Edit the current user's crontab crontab -eWhen editing the crontab command, you can use the vim editor to edit. For the use of the vim editor, please refer to using vim to edit and create new files under Linux.
The basic format of the crontab command is as follows:
* * * * * commandThe five * represent minutes, hours, days, months, and weeks respectively. The value range is as follows:
Number range | 0~59 | 0~23 | 1~31 | 1~12 | 0~7 | Commands to be executed |
The 0 and 7 of the week both represent Sunday (Sunday).
For example, restart the VPS at 0:00 every day: 0 0 * * * reboot
Commonly used auxiliary characters are explained as follows:
*(asterisk) | means accepted at any time. For example, if the day, month, and week in Example 1 are all *, it means that subsequent commands will be executed regardless of the month or day of the week at 12:00. |
,(comma) | represents the separation period. For example, if the work to be performed is at 3:00 and 6:00, it will be: 0 3,6 * * * The command time still has five columns, but the second column is 3,6, representing 3 and 6 Applicable to all |
-(minus sign) | represents a period of time. For example, a job is performed every 20 minutes between 8 o'clock and 12 o'clock: 20 8-12 * * * command. Carefully see that the second column becomes 8- 12. Represents the meaning applicable to 8, 9, 10, 11, and 12 |
/n(slash) | The n represents a number, which means every n unit interval. For example, once every five minutes, then: */5 * * * * The command is matched with * and /5, or it can be written as 0-59/ 5. Same meaning |
crontab instance
The following is an example of crontab execution every minute, every hour, every day, every week, every month, and every year:
- crontab is executed every 3 minutes */3 * * * *
- crontab is executed once every hour 0 * * * *
- crontab is executed once a day 0 0 * * *
- crontab is executed once a week 0 0 * * 0
- crontab is executed once a month 0 0 1 * *
- crontab is executed once a year 0 0 1 1 *
Recommended site searches: domain name space registration, ip query, US server rental, overseas host rental, my ip address, ip rental, Qingdao ip agent Hong Kong cloud host, Ministry of Industry and Information Technology icp registration number, me domain name registration< /p>
发表评论