By lanf, source: www.xiaoxiang.net
Author: Fred Huang
Just made a backup module, and later the master prompts to add a function of automatic backup, and study CRON this service. CRON is Linux's built-in service, but it does not automatically, you can start with the following method, close this service: / sbin / service crred start // Startup service
/ SBIN / Service Crond Stop // Turn off the service
/ sbin / service crd restart // Restart service
/ sbin / service crd reload // Replacement configuration
You can also start this service automatically when the system is started: in /etc/rc.d/rc.local, the end of this script:
/ sbin / service crd Start
Now the CRON this service is in the process, we can use this service, and the cron service provides the following interfaces for everyone:
1. Edit directly with crontab command
The CRON service provides a crontab command to set the cron service. Here is some of the parameters and descriptions of this command:
crontab -U / / Set a user's cron service, the general root user needs this parameter when executing this command
crontab -l // lists the details of a user CRON service
crontab -r // Delete CRON services without a user
Crontab-E / / Edit a user's CRON service
For example, root looks at your own CRON settings: crontab -u root -l
Another example, root wants to delete the Fred CRON settings: crontab -u fred -r
When editing the cron service, the edited content has some formats and conventions, enters: crontab -u root -e
Enter the VI edit mode, the content of the edited must meet the following format: * / 1 * * * ls >> / TMP/LS.txt
The previous part of this format is the set of time. The following part is the command to be executed. If you want to execute too much, you can write these commands to a script, then call this script directly, call I remember to write the full path to the command. Time setting We have some agreement, the previous five * represent five numbers, the number of values and meanings of the numbers are as follows:
Minute (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week (0-6) // 0 represents Sunday
In addition to the numbers, there are several special symbols "*", "/", ",", ", * represents all the numbers within the range," / "represents every meaning," * / 5 "Indicates that every 5 units," - "represents from a number to a number,", "separate several discrete numbers. The following example shows the problem:
6 o'clock in the morning
0 6 * * * Echo "Good Morning." >> /TMP/test.txt // Note Simple Echo, I can't see any output from the screen, because Cron puts any output email to the root mailbox.
Every two hours
0 * / 2 * * * echo "Have a break now." >> /tmp/test.txt
Every two hours from 11:00 pm, 8:00 in the morning
0 23-7 / 2, 8 * * * Echo "Have a good Dream :)" >> /TMP/test.txt Every month, the worship of each worship, 11 points in the morning
0 11 4 * 1-3 Command Line
At 4 o'clock on January 1
0 4 1 1 * Command Line
After each editing a user's cron setting, CRON automatically generates a file as the same name with this user in / var / spool / cron. This user's CRON information is recorded in this file. This file is not directly edited. Only CRONTAB -E can be edited. After CRON starts, read this file every time, check whether you want to perform the commands. So this file does not need to restart the CRON service after modification.
2. Edit / etc / crontab file configuration cron
Cron service not only reads all the files in / var / spool / cron per minute, but also reads / etc / crontab, so we can use Cron service to do something through the CRON service. The use of crontab is for a user, while editing / etc / crontab is a task for the system. The file format of this file is:
Shell = / bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin
Mailto = root // If there is an error, or there is data output, the data is sent as a message to this account.
Home = / / / The path runs, here is the root directory
# Run-Parts
01 * * * * root run-parts /etc/cron.Hourly // Script in /etc/cron.Hourly per hour
02 4 * * * root run-parts /etc/cron.daily / / Script in /etc/cron.daily per day
22 4 * * 0 root run-parts /etc/cron.warekly / / Script in /etc/cron.warekly per week
42 4 1 * * root run-parts /etc/cron.monthly / / monthly to perform scripts in /etc/cron.monthly
Everyone pays attention to "Run-Parts" parameters. If this parameter is removed, you can write a certain script name to run, not the folder name.