Before learning the crontab command, do you see any of the following scenarios:
(1) The test server will generate a lot of junk files every day, manual daily to clean up very cumbersome, how to let the system automatically clean up on time?
(2) How to collect the key information of the server regularly?
With the crontab command, you can complete the timed execution of the script. Let's take a look at the usage of the crontab command
1. Related fields in Crontab
Points < > < > Day < > Month < > Week < > Commands to run
where < > represents spaces, a total of 6 columns. We understand what each column in crontab means:
1th column min (1~59)
2nd Column hour (1~23,0 = midnight)
3rd Liege (1~31)
4th month (1~12)
5th column of the Week (0~6,0) (Sunday)
6th List of commands to run (use absolute path)
In these fields, you can use the bar "-" to represent a time range, for example, if you want to run a job from Monday to Friday, you can represent it using 1-5 in the day of the week. You can also use the comma "," in these fields, for example, if you want to run a job in Monday and Thursday, you only need to use 1, 4来. You can use an asterisk * to represent a continuous period of time. If you do not have a special qualification for a field that represents time, you should also fill in the field with *. Each entry for the file must contain 5 time fields, separated by a space between each domain. All comment lines in the file are represented by # at the beginning of the row.
2. crontab Example
(1) 21:30 run/apps/bin/cleanup.sh script per night
21* * */apps/bin/cleanup.sh
(2) 4:45 run/apps/bin/backup.sh script on 1, 10, 22nd per month
4 1,10,22 * */apps/bin/backup.sh
(3) 1:10 run the Find command on Saturdays, Sunday
1 * * 6,0/bin/find-name "core"-exec rm {} \;
(4) Run/apps/bin/dbcheck.sh scripts every 30 minutes from 18:00 to 23:00 every day
0,30 18-23 * * */apps/bin/dbcheck.sh
3. crontab command Options
Based on the above, we know the configuration rules for cron individual tasks, so this section is about how to use the crontab command to add, modify, and delete tasks for a task.
The general form of the crontab command is: crontab [-u user]-e-l-r
which
-u user name.
-e Edit the crontab file.
-l lists the contents of the crontab file.
-R Delete the crontab file.
If you log in with your own name, you do not have to use the-u option because the command knows the current user when executing the crontab command.
3.1 Create a new crontab file
Create a file named <user>cron, where <user> is the user name, such as Davecron. Add the following to the file
0,15,30,45 18-06 * * */bin/echo ' date ' >/dev/console
Save and exit. Make sure that the first 5 fields are separated by spaces. In the example above, the system will output the current time to the console every 15 minutes. If the system crashes or hangs, you can see at what time the system stopped working at the last displayed time.
In order to submit the crontab file you just created, you can use this newly created file as a parameter to the crontab command:
$ crontab Davecron
Now that the file has been submitted to the Cron process, it will run every 15 minutes. At the same time, a copy of the newly created file has been placed in the/var/spool/cron directory, and the file name is the user name (that is, Dave).
3.2 List crontab files
In order to list crontab files, you can use:
$crontab-L
0,15,30,45 18-06 * * */bin/echo ' date ' >/dev/console
To prevent accidental deletion of the crontab file, you can use the following method to make a backup of the crontab file:
$ crontab-l > $HOME/mycron
3.3 Edit Crontab file
$ Crontab-e
At this point we can modify the crontab file and exit as if using VI to edit any other file. If you modify some entries or add new entries, Cron makes the necessary integrity checks when you save the file. If one of the fields has a value that exceeds the allowable range, it will prompt you. When we edit the crontab file, we may not be adding a new entry. For example, add one of the following:
#DT: Delete Core files,at3,30am on 1,7,14,21,26 days of each month
3 1,7,14,21,26 * */bin/find-name "core"-exec rm {} \;
Save and exit. It is best to add a comment above each entry in the crontab file, so that you can know its function, run time, and, more importantly, what user's job it is.
Now let's use the previous crontab-l command to list all of its information:
$ crontab-l
0,15,30,45 18-06 * * */bin/echo ' date ' >/dev/console
#DT: Delete Core files,at3,30am on 1,7,14,21,26 days of each month
3 1,7,14,21,26 * */bin/find-name "core"-exec rm {} \;
3.4 Delete crontab file
$ crontab-r
[Shell programming] how to use crontab