Article from: http://www.blogjava.net/decode360/archive/2009/09/18/287743.html
Decode360 's Blog teacher (diligence and desolate qq:150355677 Msn:[email protected])
Rookie said not to write shell script, but still can understand, yesterday because passed too much data, the result Namenode entered Safe mode, and then the small partner hurriedly suspended every day 4 o'clock in the morning automatic data merge compression and transfer script, today I myself ready to change, only to find this blog post.
First talk about my own changes in the process, through the teacher's blog, directly into the/var/spool/cron/user directory, directly modify the corresponding script execution time, originally #0 4 * * * bash/opt/src/shell-src/backuplog.sh is this , and then I removed the comment from the front, so it can be automatically executed at 4 tomorrow, and I don't have to do it manually. My blog layout is too ugly, we still go to the original address to see it
Here is the teacher's blog, pure Dry, super useful:
Linux Execute script under Timer
Today I made a backup script of the database, and by the way the system had to learn the settings of the timed execution script under Linux. Linux scheduled execution is mainly used in the crontab file to add a custom plan to execute, the setup is slightly more complicated than Windows (because there is no graphical interface), but it is not very complex, basically used once to remember, the key is to remember/var/spool/ Cron this directory. Here's a look at the specific usage:
First look at the/etc/crontab file:
$ cat/etc/crontab
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root
home=/
# Run-parts
* * * * * Root run-parts/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly
The first four rows are the environment variables that set the cron task to run. The value of the shell variable specifies the shell environment used by the system (the example is Bash shell), which defines the path to execute the command. The output of Cron is sent as an e-mail message to the user name defined by the mailto variable. If the MAILTO variable is defined as an empty string (mailto= ""), the e-mail message is not sent. The home variable can be used to set the base directory when executing a command or script.
Each line of the task in the file/etc/crontab is described in the following format:
minute hour day month DayOfWeek command
minute -integers from 0 to 59
hour -integers from 0 to 23
Day-an integer from 1 to 31 (must be a valid date for the specified month)
month -an integer from 1 to 12 (or a month such as the Jan or Feb abbreviation)
DayOfWeek -integers from 0 to 7, 0 or 7 to describe Sunday (or as represented by sun or mon shorthand)
Command-commands to execute (commands that can be used as Ls/proc >>/tmp/proc or execute custom scripts)
Root indicates to run as root user
Run-parts means a folder followed by all the scripts under that folder
For each of these statements, the asterisk (*) represents all available values. For example, when referring to month, the command is executed monthly (subject to other restrictions).
The hyphen (-) between integers denotes an integer column, for example 1-4 means an integer 1,2,3,4
The specified value is separated by commas. Such as: 3,4,6,8 represents these four specified integers.
The symbol "/" specifies the stepping setting. "/<interger>" indicates a stepping value. such as the 0-59/2 definition is executed every two minutes. The step value can also be represented by an asterisk. such as */3 is used to run a specified task every three months running.
A comment line that begins with "#" is not executed.
If a cron task needs to be performed on a regular basis instead of by the hour, day, week, and month, you need to add the/ETC/CRON.D directory. All files and files in this directory are/etc/crontab in the same syntax, see the sample:
# Record the memory usage of the system Everymonday
# at 3:30AM in the File/tmp/meminfo
3 * Mon cat/proc/meminfo >>/tmp/meminfo
# Run Custom Scrīpt the first day of every month at 4:10AM
4 1 * */root/scrīpts/backup.sh
Users other than the root user can perform crontab configuration scheduled tasks. All user-defined crontab are stored under directory/var/spool/cron, and the task is executed as the creator. To create a crontab with a specific user, first log in as that user, execute command crontab-e, and the system initiates the editing crontab that is specified in visual or editor. The file content is the same as the/etc/crontab format. Examples are as follows:
0 3 * * */home/dbbackup/db1backup.sh Backup
0 4 * * */home/dbbackup/db2backup.sh Backup
Represents 3 points per day to perform/home/dbbackup/db1backup.sh backup,4 point execution/home/dbbackup/db2backup.sh Backup, if it is executed every five minutes can be changed to:
*/5 * * * */home/dbbackup/db2backup.shbackup
When the changed crontab needs to be saved, the file is saved in the file/var/spool/cron/username as follows. The file name differs depending on the user name.
The Cron service checks for changes in/etc/crontab,/etc/cron.d/,/var/spool/cron files every minute. If a change is found, it is downloaded to the memory. Therefore, even if the crontab file changes, the program does not need to be restarted. The recommended custom task is to use the CRONTAB-E command to add, exit after the/etc/init.d/crondrestart command to restart the Crond process, the official file said not to restart the process, but I do not restart the task can not run the situation. Start do not know/etc/crontab file run-parts what meaning, direct command in accordance with the/etc/crontab format plus always can not run, later only know run-parts refers to the folder followed by.
The following is attached to an introduction:
************************************************************************************
Cron is a timed execution tool under Linux that can run a job without human intervention. Since Cron is a built-in service for Linux, it does not automatically get up, and you can start and shut down this service in the following ways:
/sbin/service Crond Start//Startup service
/sbin/service Crond stop//Shut down service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
You can also start the service automatically when the system starts:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now cron This service is already in the process, we can use this service, Cron service provides the following kinds of interfaces for everyone to use:
1. edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, which is usually required by the root user when executing this command
CRONTAB-L//list details of a user cron service
Crontab-r//Delete a cron service with no users
CRONTAB-E//Edit a user's cron service
For example, root to view your cron settings: Crontab-u root-l
Again, for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing the Cron service, the edited content has some formatting and conventions, input: Crontab-u root-e
Enter VI edit mode, the content of the edits must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
The first part of this format is the time setting, the next part is the command to execute, if you want to execute too many commands, you can write these commands into a script, and then call this script directly here, you can recall the full path of the command when the call. Time setting we have a certain agreement, the preceding five * number represents five numbers, the value range and meaning of the numbers are as follows:
Minutes (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week (0-6)//0 on behalf of Sunday
In addition to the numbers there are several special symbols are "*", "/" and "-", ",", * represents all the values within the range of the number, "/" for each meaning, "*/5" means every 5 units, "-" represents from a number to a number, "," separate several discrete numbers. Here are a few examples to illustrate the problem:
Every morning at 6.
-----------------
0 6 * * * echo "Good morning." >>/tmp/test.txt//Note simply Echo, there is no output from the screen, because Cron will email any output to the root mailbox.
Every two hours
-----------------
0 */2 * * * echo "have a breaknow." >>/tmp/test.txt
Every two hours between 11 o'clock and 8 in the morning, eight in the morning.
-----------------
0 23-7/28 * * * echo "a good Dream:)" >>/tmp/test.txt
Every month, number 4th and Monday to Sunday, three a.m., 11.
-----------------
0 4 * 1-3 command line
January 1 morning, 4.
-----------------
0 4 1 1 * command line
After editing a user's cron settings, Cron automatically generates a file with the same name as the user under/var/spool/cron, and the cron information for this user is recorded in this file, which cannot be edited directly and can only be edited with CRONTAB-E. After Cron starts, read the file once every one of the clocks, and check to see if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.
2. Edit the /etc/crontab file configuration cron
Cron service every minute not only to read all the files within/var/spool/cron, but also to read a/etc/crontab, so we configure this file can also use the Cron service to do something. The crontab configuration is for a user, while the edit/etc/crontab is a task for the system. The file format for this file is:
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If an error occurs, or if there is data output, the data is sent to this account as an email
home=///user-run path, here is the root directory
# Run-parts
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of scripts within/etc/cron.daily
4 * * 0 root run-parts/etc/cron.weekly//weekly execution of scripts within/etc/cron.weekly
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute scripts within/etc/cron.monthly
Attention to the "run-parts" This parameter, if you remove this parameter, you can later write to run a script name, not the folder name.
************************************************************************************
Timed execution scripts under Linux (go from Decode360)