http://www.jb51.net/LINUXjishu/151805.htmlIf we need to perform periodic repetitive tasks on a regular basis, we will use the Timed task function to help us automate recurring recurring tasks such as daily, weekly and so on, without the need for human intervention. Method/Step
1, we use crontab–e to create their own scheduled tasks
# CRONTAB-E
Enter the timer task command in the crontab file file, the command syntax is as follows
* * * * * execute command (i.e. minute hour day Month Week command)
: Wq
Asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.
Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"
Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"
Forward slash (/): You can specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.
Example:
Example 1: command is executed every 1 minutes
Command:
* * * * * command
Example 2:3rd and 15 minutes per hour of execution
Command:
3,15 * * * command
Example 3: Execution at 3rd and 15 minutes from 8 o'clock in the morning to 11.
Command:
3,15 8-11 * * command
Example 4:3rd and 15 minutes of every two-day 8 o'clock in the morning to 11-point execution
Command:
3,15 8-11 */2 * command
Example 5:3rd and 15 minutes of each Monday from 8 o'clock in the morning to 11.
Command:
3,15 8-11 * * 1 command
Example 6:21:30 restart of SMB per night
Command:
* * * * */ETC/INIT.D/SMB restart
Example 7:4:45 restart SMB per month for 1, 10, 22nd
Command:
4 1,10,22 * */ETC/INIT.D/SMB restart
Example 8:1:10 restart SMB per Saturday, Sunday
Command:
1 * * 6,0/ETC/INIT.D/SMB restart
Example 9: Restart SMB every 30 minutes from 18:00 to 23:00 daily
Command:
0,30 18-23 * * */ETC/INIT.D/SMB restart
Example 10: Every Saturday night at 11:00am restart SMB
Command:
0 * * 6/ETC/INIT.D/SMB restart
Example 11: Restart SMB every hour
Command:
* */1 * * * */ETC/INIT.D/SMB restart
Example 12: Restart SMB every hour from 11 o'clock to 7 in the morning
Command:
* 23-7/1 * * * */ETC/INIT.D/SMB restart
Example 13:4th per month with 11 points per Monday to Wednesday restart SMB
Command:
0 4 * MON-WED/ETC/INIT.D/SMB restart
Example 14:4-point restart of SMB on January 1
Command:
0 4 1 Jan */ETC/INIT.D/SMB restart
Example 15: Execution of scripts within/etc/cron.hourly directory per hour
Command:
* * * * * Root run-parts/etc/cron.hourly
Description
Run-parts This parameter, if you remove this parameter, you can write a script name to run, not the directory name.
Iv. precautions for use
1. Pay attention to the environment variable problem
Sometimes we create a crontab, but this task cannot be executed automatically, but it is not a problem to perform this task manually, which is usually caused by not configuring environment variables in the crontab file.
When defining multiple dispatch tasks in a crontab file, one of the issues that needs special attention is the setting of environment variables, because when we perform a task manually, it is done in the current shell environment, the program can certainly find the environment variable, and the system will not load any environment variables when it automatically executes the task schedule. Therefore, you need to specify all the environment variables that are required for the task to run in the crontab file, so that the system does not have a problem when it executes the Task Scheduler.
Don't assume that Cron knows the special circumstances you need, and it doesn't really know. So you have to make sure that you provide all the necessary path and environment variables in the shelll script, except for some auto-set global variables. So note the following 3 points:
1) Write the global path when the file path is involved in the script;
2) When script execution is used in Java or other environment variables, the environment variables are introduced through the source command, such as:
The code is as follows: Cat start_cbp.sh
#!/bin/sh
Source/etc/profile
Export run_conf=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh-c MeV &
3) When the script is executed manually, but crontab is not executed. At this point, we must boldly suspect that environmental variables are the bane, and can try to directly introduce environmental variables in crontab to solve the problem. Such as:
The code is as follows: 0 * * * *. /etc/profile;/bin/sh/var/www/java/audit_no_count/bin/restart_audit.sh
2. Take care to clean up the mail log for system users
Each task is scheduled to execute, the system will send the task output information in the form of e-mail to the current system users, so the cumulative, log information will be very large, may affect the normal operation of the system, so it is important to redirect each task.
For example, you can set the following form in the crontab file, ignoring the log output:
0 */3 * * */usr/local/apache2/apachectl restart >/dev/null 2>&1
"/dev/null 2>&1" means that the standard output is redirected to/dev/null and then the standard error is redirected to standard output, and standard errors are redirected to/dev/null because the standard output has been redirected to/dev/null. This will solve the problem of log output.
3. System-level task scheduling and user-level task scheduling
System-level task scheduling is mainly to complete some maintenance operations of the system, user-level task scheduling is mainly to complete the user-defined tasks, you can put the user-level task scheduling to the system-level task scheduling to complete (not recommended), but in turn, the root user's task scheduling operation can be through the "crontab– Uroot–e "To set, you can also write the dispatch task directly to the/etc/crontab file, it should be noted that if you want to define a scheduled restart of the system task, you must put the task into the/etc/crontab file, Even the task of creating a timed restart of the system under the root user is not valid.
4. Other precautions
The newly created cron job will not execute immediately, at least 2 minutes. If you restart Cron, it will be executed immediately.
When the crontab suddenly fails, you can try/etc/init.d/crond restart solve the problem. Or check the log to see if a job has execution/error tail-f/var/log/cron.
Don't run crontab-r. It removes the user's crontab file from the crontab directory (/var/spool/cron). All crontab of the user have been deleted.
In Crontab, the% has a special meaning, indicating the meaning of the line break. If you want to use the words must be escaped \%, such as the frequently used date ' +%y%m%d ' in the crontab will not be executed, should be replaced by the date ' +\%y\%m\%d '.
Linux crontab implementation of the timing service detailed