Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
The previous several articles talked about some operation commands about time in Linux system, including date, at, sleep, etc. This article will talk about the last command about time, that is crontab, the ultimate killer.
1. Basic Concepts
Crontab is just a command, a very powerful command, to execute the program on time, and by time, we can execute it over and over again, every hour, every minute, every second, every day at some point in the day, and so on.
This works by reading and modifying a file called crontab. This file contains a list of programs that need to be executed periodically, as well as The Times when they should be repeated.
In fact, there are two commands for crontab, one is called crontab, which is used to modify the crontab file, and the other is called cron, where the user actually executes the scheduled program.
It is also important to note that for each user, there is a separate crontab file with user-level isolation.
Two, basic use
Before using it, ensure that it is installed. If it is not installed, run the following command.
# centos install
$sudo yum install vixie-cron crontabs
$chkconfig crond on
$service crond start
# Debian install
$sudp apt install cron
$service cron restart
Copy the code
After the installation, you can perform simple operations.
$crontab -l $crontab -lCopy the code
The output is as follows:
Since this is a new server and I haven’t used the crontab command yet, I don’t have this file.
We don’t have this file, so let’s create this file.
$crontab -e $crontab -eCopy the code
When editing the contents of this file, observe the following syntax rules for each scheduled task.
# m h dom mon dow command
x x x x x command
Copy the code
- The first x: represents minutes (0-59)
- The second x: represents hours (0-23)
- Third X: Delegate Day (1-31)
- Fourth X: represents the month (1-12)
- The fifth x: represents the day of the week (0-6, Sunday is 0)
- Command: That is the command that needs to be executed
Also, if you don’t want to fill the first five x’s with any value, you can fill them with an asterisk (*), which represents any value.
Also, if you don’t want to perform a task, you can save the file with a # before it.
The command output enters the editing state
$crontab -e
Copy the code
Enter content in a file
10 21 * * * touch ~/new_file
Copy the code
Then save the file and the console prints as follows.
It means a new one has been installedcrontabFile.
What if you don’t want to go on this mission? First, you can add # to the task as above, or delete the crontab file directly.
$crontab -r $crontab -rCopy the code
These are the most common uses of crontab, see the help manual for more.
Three, more usage
48 * * * * command
0 0 * * 2 command
30 5 1-16 * * command
0 0 * * 1,3,5 command
0 */4 * * * command
*/5 * * * 1-4 command
Copy the code
So how does the timing task above work? Leave your answers in the comments section!