February 25, 2017, Saturday crontab scheduled task 19. Crontab Scheduled Tasks
With the crontab command, we can execute specified system instructions or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more. This command works well for periodic log analysis or data backup.
19.1. Command format
crontab [-u user] File crontab [-u user] [-e |-l |-r]
19.2. Command parameters
- -u User: Used to set a user's crontab service;
- File:file is the name of the command file, which indicates that file is the Crontab task list and loaded into crontab. If this file is not specified on the command line, the crontab command will accept the commands typed on the standard input (keyboard) and load them into crontab.
- -E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited.
- -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, if no user is specified, deletes the current user's crontab file by default.
- -I: Give a confirmation prompt when deleting a user's crontab file.
19.3. crontab file formats
Time-sharing day and month command to run
- 1th column min 0~59
- 2nd Hour 0~23 (0 = midnight)
- 3rd Liege 1~31
- 4th Column Month 1~12
- 5th column of the week 0~7 (0 and 7 = Sunday)
- 6th List of commands to run
19.4. Common methods to create a new crontab file
Before submitting a crontab file to the cron process, 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, and if you do, then you edit the $home directory. Profile file in which to add such a line:
Editor=vi; Export EDITOR
Then save and exit. You might want to create a file named <user> Cron, where <user> is the user name, for example, Davecron. Add the following to the file.
# (Put your own initials here) echo the date to the console every# 15minutes between 6pm and 6am0,15,30,45 18-06 * * */bin /echo ' Date ' >/dev/console
Save and exit. Note the preceding 5 fields are separated by a space.
In the example above, the system will output the current time to the console every 1 to 5 minutes. If the system crashes or hangs, you can see at what time the system stopped working at the last displayed time. In some systems, the use of tty1 to represent the console, according to the actual situation of the above example can be modified accordingly. In order to submit the crontab file you just created, you can use this newly created file as a parameter to the cron command:
$ crontab Davecron
Now that the file has been submitted to the Cron process, it will run every 1 5 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).
List crontab files
Use the-l parameter to list the crontab file:
$ crontab-l0,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
This way, once the crontab file is accidentally deleted, it can be recovered quickly using the method described in the previous section.
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 use VI to edit the crontab file:
$ crontab-e
You can modify the Crontab file and exit as you would edit any other file using VI. 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,at 3.30am on 1,7,14,21,26,26 days of each month30 3 1,7,14,21,26 * */bin/find-name ' core '-exec R m {} \;
Save and exit.
Annotations
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 timed job it is.
Delete crontab file
$crontab-R
19.5. Use instance Instance 1: Execute once every 1 minutes mycommand
* * * * * * mycommand
Example 2:3rd and 15 minutes per hour of execution
3,15 * * * * mycommand
Example 3: Execution at 3rd and 15 minutes from 8 o'clock in the morning to 11.
3,15 8-11 * * * mycommand
Example 4:3rd and 15 minutes of every two-day 8 o'clock in the morning to 11-point execution
3,15 8-11 */2 * * mycommand
Example 5:3rd and 15 minutes of every Monday 8 o'clock in the morning to 11 point of execution
3,15 8-11 * * 1 mycommand
Example 6:21:30 restart of SMB per night
* * * * */ETC/INIT.D/SMB restart
Example 7:4:45 restart SMB per month for 1, 10, 22nd
4 1,10,22 * */ETC/INIT.D/SMB restart
Example 8:1:10 restart SMB per Saturday, Sunday
1 * * 6,0/ETC/INIT.D/SMB restart
Example 9: Restart SMB every 30 minutes from 18:00 to 23:00 daily
0,30 18-23 * * */ETC/INIT.D/SMB restart
Example 10: Every Saturday night at 11:00am restart SMB
0 * * 6/ETC/INIT.D/SMB restart
Example 11: Restart SMB every hour
* */1 * * * */ETC/INIT.D/SMB restart
Example 12: Restart SMB every hour from 11 o'clock to 7 in the morning
0 23-7 * * */ETC/INIT.D/SMB restart
19.6. Precautions for use of environmental variables
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 you define multiple dispatch tasks in a crontab file, you need to make a special note of the settings of the environment variables, because when we perform a task manually, it is in the current shell environment, the program can certainly find the environment variable, and the system automatically executes the task schedule, it will not load any environment variables, 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:
Write the global path when the file path is involved in the script;
script execution when Java or other environment variables are used , the environment variables are introduced through the source command, such as:
cat Start_cbp.sh!/bin/shsource/etc/profileexport run_conf=/home/d139/conf/ Platform/cbp/cbp_jboss.conf/usr/local/jboss-4.0.5/bin/run.sh-c MeV &
When you manually execute the script OK, but crontab is not executed, it is likely that the environment variables, you can try to directly introduce environment variables in crontab to solve the problem. Such as:
0 * * * *. /etc/profile;/bin/sh/var/www/java/audit_no_count/bin/restart_audit.sh
Note Clean up the message 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.
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.
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 crontab fails, you can try/etc/init.d/crond Restart to 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 is not executed, should be replaced by the date ' +%y%m%d '.
You need to restart Cron after updating the system time time zone, and the service name is cron in Ubuntu:
$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
Previous
Source: http://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/crontab.html
Null
Crontab timed Task _net