With the crontab command, we can execute the specified system instructions or shell script scripts at a fixed interval of time. The unit of time interval can be any combination of minutes, hours, days, months, weeks, and more. This command is ideal for periodic log analysis or data backup. 19.1. command Format
crontab [-u user] [-e |-l |-r] 19.2. Command parameter -u user: Used to set a user's crontab service; File:file is the name of the command file that represents the task column as Crontab Table file and load crontab. If this file is not specified on the command line, the crontab command accepts the commands typed on the standard input (keyboard) and loads them into crontab. -E: Edit the contents of a user's crontab file. If you do not specify a user, edit the current user's crontab file. -L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified. -r: Deletes a user's crontab file from the/var/spool/cron directory, and deletes the current user's crontab file by default if no user is specified. -I: Give a confirmation prompt when deleting a user's crontab file. 19.3. crontab file Format
Time-Sharing Week the command to run the 1th column minutes 1~59 2nd hour 1~23 (0 means midnight) 3rd Liege 1~31 4th 1~12 5th 0~6 (0 for Sunday) 6th column to run the command 19.4. Common methods to create a new crontab file
Before submitting a crontab file to a cron process, you first set the environment variable editor. The cron process depends on it to determine which editor to use to edit the crontab file. 9 9 of UNIX and Linux users use VI, if you do the same, then you edit the $home directory. Profile file in which to add one line:
Editor=vi; Export EDITOR
Then save and exit. You may wish to create a file named <user> Cron, where <user> is the user name, for example, Davecron. Add the following content to the file.
# (Put your own initials) echo the ' date to the console every
# 15minutes between 6pm and 6am
0,15,30,45 18-06 * * */bin/echo ' date ' >/dev/console
Save and exit. Note that the first 5 fields are separated by spaces.
In the example above, the system will output the current time to the console every 1 5 minutes. If the system crashes or hangs, you can see at what time the system stopped working from the last time it was displayed. In some systems, using tty1 to represent the console, the above example can be modified according to the actual situation. To submit the crontab file you just created, you can use this newly created file as a parameter to the cron command:
$ crontab Davecron
The file is now submitted to the Cron process, which will run every 1 to 5 minutes. Also, a copy of the newly created file has been placed in the/var/spool/cron directory, and the filename is the username (that is, Dave). list crontab Files
Use the-l parameter to list crontab files:
$ crontab-l 0,15,30,45,18-06 * * * *
/bin/echo ' date ' > dev/tty1
You can use this method to make a backup of the crontab file in the $home directory:
$ crontab-l > $HOME/mycron
In this way, once you accidentally delete the crontab file, you can use the method described in the previous section to recover quickly. Edit crontab file
If you want to add, delete, or edit entries in the Crontab file, and the editor environment variable is set to VI, you can edit the crontab file with VI:
$ crontab-e
You can modify the Crontab file and exit as you would any other file using VI editing. If you modify some entries or add new entries, Cron will perform the necessary integrity checks on the file when you save it. If one of the fields has a value that is outside the allowable range, it prompts you. When we edit the crontab file, we may add a new entry. For example, add the following article:
# Dt:delete Core Files,at 3.30am on 1,7,14,21,26,26 for each month
3 1,7,14,21,26 * */bin/find-name ' core '-E xec rm {} \;
Save and exit.
Annotations
It's a good idea to add a comment above each entry in the crontab file so that you know its function, its running time, and more importantly, who knows which user's timed job. Delete crontab file
$crontab-R
19.5. Use examples
Example 1: Perform a mycommand every 1 minutes
* * * * * mycommand * * *
Example 2:3rd and 15 minutes per hour execution
3,15 * * * * mycommand
Example 3:3rd and 15 minutes from 8 o'clock in the morning to 11.
3,15 8-11 * * * * mycommand
Instance 4:3rd and 15 minutes from 8 o'clock in the morning to 11 points every two days
3,15 8-11 */2 * * mycommand
Example 5:3rd and 15 minutes per Monday 8 o'clock in the morning to 11 points of execution
3,15 8-11 * * 1 mycommand
Example 6:21:30 restart SMB per night
* * * */ETC/INIT.D/SMB restart
Instance 7:4:45 Restart SMB for 1, 10, 22nd per month
4 1,10,22 * */ETC/INIT.D/SMB restart
Example 8:1:10 restart SMB per Saturday and Sunday
1 * * 6,0/ETC/INIT.D/SMB restart
Example 9: Restart SMB every 30 minutes from 18:00 to 23:00 every day
0,30 18-23 * * * */ETC/INIT.D/SMB restart
Example 10: Every Saturday night 11:00 pm reboot SMB
0 * * 6/ETC/INIT.D/SMB restart
Example 11: Restart SMB per hour
* */1 * * * */ETC/INIT.D/SMB restart
Example 12: Restart SMB every hour between 11 o'clock and 7 in the morning.
* 23-7/1 * * * */ETC/INIT.D/SMB restart
19.6. Precautions for use
attention to environment variable problem
Sometimes we create a crontab, but this task is not automated, but it is not a problem to perform this task manually, which is typically caused by not configuring the environment variable in the crontab file.
When you define multiple scheduling tasks in a crontab file, you need to specifically set the environment variable, because when we perform a task manually, it is in the current shell environment, and the program can certainly find the environment variable, and the system does not load any environment variables when it performs task scheduling automatically, so You need to specify all the environment variables required for the task to run in the crontab file, so that there is no problem when the system performs task scheduling.
Do not assume that Cron knows the special circumstances that are required, it does not actually know. So you have to make sure that you provide all the necessary path and environment variables in the Shelll script, in addition to some of the global variables that are automatically set. So notice the following 3 points:
Write the global path when the file path is involved in the script;
When a script executes to use Java or other environment variables, the source command is used to introduce environment variables, such as:
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 manual execution of the script OK, but crontab is not executed, it is likely that the environment variable, you can try to crontab directly into the environment variables to solve the problem. Such as:
0 * * * *. /etc/profile;/bin/sh/var/www/java/audit_no_count/bin/restart_audit.sh
Note cleaning the system user's mail log
Each task scheduled execution completed, the system will send the task output information through e-mail to the current system users, so that the accumulation of 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 to ignore the log output:
0 */3 * * */usr/local/apache2/apachectl restart >/dev/null 2>&1
The "/dev/null 2>&1" means that the standard output is redirected to the/dev/null first, then the standard error is redirected to the standard output, and the standard error is redirected to the/dev/null because the standard output has been redirected to the/dev/null. This will solve the problem of log output. system-level task scheduling and user-level task scheduling
System-level task scheduling mainly completes some maintenance operations of the system, user-level task scheduling mainly completes a number of user-defined tasks, you can put user-level task scheduling to the system-level task scheduling to complete (not recommended), but the reverse is not, the root user task scheduling operations can be through the "crontab– Uroot–e "To set up, you can also write the dispatch task directly to the/etc/crontab file, it should be noted that if you want to define a task that periodically restarts the system, you must put the task in the/etc/crontab file. Even the task of creating a timed reboot system under the root user is not valid. Other considerations
The newly created Cron job will not be executed immediately and will take at least 2 minutes to execute. If you restart Cron, it executes immediately.
When crontab fails, you can try/etc/init.d/crond Restart to solve the problem. Or check the log to see if a job has executed/error tail-f/var/log/cron.
Don't run crontab-r. It deletes the user's crontab file from the crontab directory (/var/spool/cron). All crontab that deleted the user are gone.
In Crontab, the% is of special meaning, indicating the meaning of line wrapping. If you want to use it must be escaped%, such as the frequent use of the date ' +%y%m%d ' in the crontab will not be executed, should be replaced by the date ' +%y%m%d '.
After you update the system time zone, you need to restart Cron, and in Ubuntu the service name is cron:
$service Cron Restart
Start, stop, and restart Cron under Ubuntu:
$sudo/etc/init.d/cron start $sudo/etc/init.d/cron stop $sudo/etc/init.d/cron restart