Linux crond scheduled task
Chapter 1 What is Crond?
Crond is a service or software used in linux to regularly execute commands or specify program tasks. Generally, after the CentOS5/6linux operating system is installed, the Crond task scheduling service is started by default. The Crond service checks the system for tasks to be executed on a regular basis (which is checked every minute by default. If yes, the scheduled task is automatically executed according to its preset scheduled task rules. This Crond scheduled Task Service is equivalent to the alarm clock we use in the morning.
Chapter 2 scheduled task scheduling in Linux crond can be divided into the following two situations:
1. scheduled tasks in linux: tasks that are periodically executed by the system, such as polling system logs, backing up system data, and clearing system caches. These tasks do not require human intervention. For example:
[Root @ web01 ~] # Ll/var/log/messages *
-Rw ------- 1 root 585506 January 24 21:32/var/log/messages
-Rw ------- 1 root 495226 December 13 14:41/var/log/messages-20151213
-Rw ------- 1 root 501046 December 20 10:33/var/log/messages-20151220
-Rw ------- 1 root 333767 December 30 20:27/var/log/messages-20151230
-Rw ------- 1 root 332996 January 10 12:19/var/log/messages-20160110
2. User-executed tasks: the tasks that a user or system administrator needs to perform on a regular basis, such as synchronizing time with the server on the Internet every 5 minutes, back up website data and database data at every night. Generally, you need to set these tasks by yourself.
[Root @ web01 ~] # Crontab-l
*/5 */usr/sbin/ntpdate time.nist.gov>/dev/null 2> & 1
# ***** Echo "yangrz">/server/log/yangrz
Chapter 4 commands
The crond service is a running program, while the crontab is a command that you use to set scheduled rules.
Crontab option
-L list (view User-Defined scheduled tasks)
-E. Edit the scheduled task configuration file.
Crontab-e === vi/var/spool/cron/root
Crontab-l === cat/var/spool/cron/root
Chapter 1 meaning of time period in crontab syntax
Segment |
Description |
Value range (integer) |
Section 1 |
Minute |
00-59 (00 can also be 0) |
Section 2 |
Hours |
00-23 |
Section 3 |
Indicates day and day |
01-31 |
Fourth paragraph |
Month |
01-12 |
Section 5 |
The day of the week. |
0-7 (both 0 and 7 indicate Sunday) |
Tip: Memory tip: time-sharing day-month week |
#. ---------------- Minute (0-59)
# |. ----------- Hour (0-23)
# |. ---------- Day of month (1-31)
# |. ------- Month (1-12) OR jan, feb, mar, apr...
# |. ---- Day of week (0-6) (Sunday = 0 or 7) OR sun, mon, tue, wed, thu, fri, sat
# |
# ***** User-name command to be executed
Chapter 1 meaning of special symbols in crontab syntax
Special symbols |
Description |
* |
* Indicates any time, which actually means "every. For example, 00 23 *** cmd indicates that the cmd task is executed every week at every month. Tip: If the scheduled task is in the hour, the task can be submitted daily at most. * In the percentile is equivalent to 0-59, indicating every minute. |
- |
Minus sign, which indicates a separator, indicating a time range and a range, such as to, every day. 00 17-19 *** cmd. That is, execute at, respectively. |
, |
Comma (,) indicates the meaning of time periods separated. 30 17,18, 19 ***/bin/sh/scripts/test. sh indicates that the/scripts/test. sh script is executed at, and every day. |
/N |
N represents a number, that is, "every n units of time". For example, if a task is executed every 10 minutes, you can write */10 * cmd, */10 indicates executing the cmd command every 10 minutes. |
Chapter 4 essentials and methods for writing scheduled tasks:
Essentials 1: add necessary comments to the scheduled task rules
Method 2: it is best to write the scheduled Task Command or program to the script for execution.
Method 3: Add/bin/sh before executing the shell script task
Essentials 4: add the command or script at the end of the scheduled task>/dev/null 2> & 1
&>/Dev/null 1>/dev/null 2>/dev/null
Essentials 5: Execute related scheduled tasks under a specified user
Tip 6: do not print output information at will in the production task program.
Tar zcf echo 123> a. log
Essentials 7: the script for scheduled task execution must be in the standard path (/service/scripts)
Essentials 8: Configure scheduled tasks to standardize the operation process
Chapter 4 configure scheduled tasks and standardize the operation process
1) first, you must operate successfully on the command line, and then copy the successful command to the script to reduce the chance of errors in each small step.
2) then test the script. After the test is successful, copy the script's standard path to the scheduled task configuration.
3) first test in the test environment, and then formally deploy the environment specifications.
Question: Package the/etc/service file of the system to/tmp for backup every two hours (it is best to back up the file to a different package each time)
Baggage:/etc/service does not exist. It should be/etc/services.
Package the content from cd to the parent directory of the target content
The package frequency is minute, and the package name must be accurate to minute.
Make sure that the command is correctly executed. Then write it to the script (copy it in)
It is best to write scheduled task commands or programs to scripts for execution.
Test script correctness (how to write a scheduled task and how to test the command line)
The script test is correct, and the command line command should also be copied.
* *** Tar zcvf/tmp/service-$
In this case, it is wrong to write the command in the configuration file of the scheduled task. Therefore, you must write the command into the file to avoid errors.
Question: at every night, delete the/tmp/services.tar.gz package from the previous day.
/Var/log/cron *
This is the crontab log. If the execution is correct or incorrect, there will be output in the log.
Chapter 4 How to debug a scheduled task:
1. Add execution frequency debugging tasks (some tasks cannot be used in the production environment without testing opportunities)
2. Code release: Personal Development Environment-office test environment-IDC room test environment-IDC formal environment (group, gated release)
3. Adjust the system time debugging task (cannot be used directly in the production environment) for 5 minutes
4. Run the script log to output debugging scheduled tasks.
5. Pay attention to the problems caused by some task commands.
6. (*/1 ****** echo "=">/tmp/oldboy. log> dev/null 2> & 1)
7. Pay attention to timing task faults caused by environment variables (java)
8. Use the crond scheduled task log to debug the scheduled task (/var/log/cron)
9. Put the script in a scheduled task. Many times the system variables cannot be obtained, which may cause problems. At this time, the system variables can be redefined in the script.
Chapter 10 concerning the production and application of crontab scheduled tasks:
1. System Environment Variables
2. Use absolute paths for scheduled tasks
3. For script permission issues, add/bin/sh
4. Escape with backslash \ % for time variables. It is best to use a script.
5.>/dev/null 2> & 1 (1>/dev/null 2>/dev/null, &>/dev/null)
6. Add notes before scheduled task rules
7. Use the script program to replace the command line scheduled task
8. Avoid unnecessary programs and command output
9. Switch to the upper-level packaging target of the target directory.
10. Use full paths (related to the identification of environment variables) for program commands in the scheduled task script ).
This article permanently updates the link address: