Linux System Timing Task Crond
What is Crond?
Crond is a service or software that is used on a Linux system to perform regular commands or to specify program tasks.
Special needs: (seconds level) Crond service can not be done, general work possessed script daemon execution (generally not used)
Why use Crond timed tasks
The timing task of the Linux system is Crond, which is equivalent to the alarm clock function in our daily life. Can meet the needs of periodic execution of tasks.
Timing Tasks for Linux systems
The work of scheduled task scheduling in Linux system can be divided into the following two kinds of situations
1.Regular tasks performed by the Linux system itself: tasks performed periodically by the system, such as polling system logs, backing up system data, cleaning up system caches, etc.
2. tasks performed by the user: for example, time synchronization is performed every 5 minutes and on the Internet time server. In general, these tasks require the user to set their own.
[[email protected] ~]# crontab-l### #time sync by Xuliangwei 2015-07-30*/5 * * * */usr/sbin/ntpdate time.windows.com > /dev/null 2>&1
Crond usage instructions for timed tasks
[[email protected] ~]# crontab --helpcrontab: invalid option -- '-' Crontab: usage error: unrecognized optionusage: crontab [-u user] file crontab [-u user] [ -e | -l | -r ] (default operation is replace, per 1003.2) -e (Edit user ' S crontab) -l (List user ' S crontab) -r (Delete user ' s crontab) -i (prompt before deleting user ' S&NBSp;crontab) -s (selinux context)
Instruction Option Description Table
Parameter meaning Specify example
-e Edit crontab file contents crontab-e-l View crontab file contents crontab-l-i Delete crontab file contents, before deleting prompts to confirm Crontab-ri-r Delete crontab file contents crontab- R-U Specifies the user that is used to perform the task Crontab-u xuliangwei-l
Hint: crontab {-L-E} is actually manipulating files such as/var/spool/cron/current user
Crontab the time period meaning table in the syntax format
Value range of paragraph meaning
The first paragraph represents minutes 00-59 the second paragraph represents the hour 00-23 the third paragraph represents the date 01-31 fourth represents the month 01-12 the fifth paragraph represents the week 0-7 (0 and 7 both represent Sunday)
crontab Syntax format special symbolic meaning table
Special Symbolic meaning
** Indicates any time, that is, the meaning of "every", for example: * * * * cmd for each of the daily 1 o'clock in the morning per month to execute the cmd command
- A minus sign, representing a delimiter, representing a time range segment, such as 17-19 points, 00 minutes per hour. 17-19 * * * cmd is the 17,18,19 the hour to execute separately
A comma that represents the meaning of the separator time period. The 20,21,22 * * * cmd means that the 20,21,22 is executed at the slightest point of the day.
/n represents a number, that is, "every n units of time" for example: Perform a task every 10 minutes */10 * * * * cmd where */10 can also be written 0-59/10
User rights and timed task files
Document Description
/etc/crontabcrontab configuration file
/etc/cron.deny The crontab command is not allowed by the users listed in this file.
/var/spool/cron All user crontab profiles are stored in this directory by default, and the file is named after the user name.
Essentials of writing timed tasks in production
Add necessary annotations to a timed task rule
[[email protected] ~]# crontab-l### #time sync by Xuliangwei 2015-07-30*/5 * * * */usr/sbin/ntpdate time.windows.com > /dev/null 2>&1
A timed Task command or program is best written to execute in a script
[Email protected] ~]# crontab-l### #backup www to/backup xuliangwei 2015-07-3000 * * */BIN/SH/SERVER/SCRIPTS/WWW_BA ckup.sh >/dev/null 2>&1
Timed task execution script to canonical path (/server/scripts)
[Email protected] ~]# crontab-l### #backup www to/backup xuliangwei 2015-07-3000 * * */BIN/SH/SERVER/SCRIPTS/WWW_BA ckup.sh >/dev/null 2>&1
Add/bin/sh before executing the shell Script task
[Email protected] ~]# crontab-l### #backup www to/backup xuliangwei 2015-07-3000 * * */BIN/SH/SERVER/SCRIPTS/WWW_BA ckup.sh >/dev/null 2>&1
Timed task command or script end plus >/dev/null 2>&1
[Email protected] ~]# crontab-l### #backup www to/backup xuliangwei 2015-07-3000 * * */BIN/SH/SERVER/SCRIPTS/WWW_BA ckup.sh >/dev/null 2>&1
Perform the relevant scheduled tasks under the specified user
It is important to pay special attention to the environment variables of different users, if the system environment variable/etc/profile is called, it is best to re-export the environment variables in the program script.
Production task program do not print output information arbitrarily
After debugging a good script, should try to debug and command output information, if you do need to output the log, can be directed to the log file, to avoid the generation of system garbage.
Configuring the Scheduled Task specification operation process
1) Configure the Scheduled task specification operation process to prevent errors.
2) first to succeed in the command operation, and then copy the successful command into the script, in each small link to reduce the chance of error
3) then test the script, after the test is successful, copy the canonical path of the script into the scheduled task configuration, do not hand knocking.
How to debug timed tasks in production
Standardize the company development and operations process:
Personal development and configuration environment → Office test Environment →IDC Room test Environment →IDC room formal environment.
Increase the task frequency to perform the debugging tasks
In the debugging, the task execution frequency speed up a bit, see if it can be normal execution, if normal, then no problem, and then change to the required task execution time.
Note: Some tasks are not allowed to execute frequently, for example: inserting data into the database at timed intervals, so that the task is tested on the test machine, and then there is less chance of a formal online problem.
Adjust system Time Debugging tasks
With the correct time to perform the task, after the setup is complete, you can modify the current time of the system to the first few minutes of the task execution time to test (or restart the scheduled task service)
Note: (cannot be used directly in production environment), guaranteed 5 minutes
Debug timed tasks with script log output
Add the log output to the script, and then hit the output into the specified log and observe the results of the log content to see if it is executed correctly.
Pay attention to some task command problems
* * * * * * echo "Xuliangwei" >>/tmp/xlw.log >/dev/null 2>&1 here Hide the task configuration is not performed correctly, because there are more >> in front, or remove the >/behind Dev/null 2>&1
Note the timing task failures caused by environment variables
For example, when debugging a Java program task, note the environment variables and add the definition of the environment variable to the script.
Debugging timed tasks by Crond Scheduled task service logs
[Email protected] ~]# tail-f/var/log/cronjul 12:15:01 student crond[7530]: (Root) CMD (/usr/sbin/ntpdate Time.window s.com >/dev/null 2>&1) Jul 12:15:29 student crontab[7527]: (root) REPLACE (root)
Scheduled tasks in production 10 simple words
System environment variable problem
Scripting permissions problem plus/bin/sh
The practice variable problem is escaped with a backslash \%, preferably using a script.
>/dev/null 2>&1 Problems
Comment Before scheduled task rule
Using a script to replace a command-line scheduled task
Avoid unnecessary program and command output
1) cut to the top level of the target directory to package the target
2) program commands in timed task scripts use full path as far as possible (related to identification of environment variables)
This article is from the "Xu Liangwei" blog, make sure to keep this source http://liangweilinux.blog.51cto.com/8340258/1681210
Linux System Timing Task Crond