Linux timed Task Service Crond (crontab)
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.
Generally, when we install the C5/C6 system, Crond will exist by default.
The Crond service default (per minute) checks the system for scheduled tasks that need to be performed and, if any, performs this scheduled task according to predefined rules. For example, the root user's timed task configuration is/var/spool/cron/root.
Crond service = Alarm Clock
By default, when a user establishes a scheduled task rule, the rule records that the corresponding configuration file exists in/var/spool/cron, and its crontab profile corresponds to the name of the logged-on user.
Crontab Timing task of the writing format is very simple, the user's timing task rules are generally divided into 6 segments (each segment through a space to separate, the system's scheduled task is/etc/crontab, divided into 8 paragraphs, spaces to separate), the first five time of the set paragraph, the sixth paragraph is to execute the command or Script task segment.
Related configuration files:
The/var/spool/cron/directory contains crontab tasks for each user, including root, and each task is named after the creator's name
/etc/crontab This file is responsible for scheduling various management and maintenance tasks.
/etc/cron.d/This directory is used to store any crontab files or scripts to be executed.
We can also put the script in the/etc/con.hourly,/etc/con.daily,/etc/con.weekly,/etc/con.monthly directory, and let it execute once per hour/day/week, month.
/etc/cron.deny The Ctrontab command is not allowed by the users listed in this file.
/etc/cron.allow the users listed in this file are allowed to use the crontab command, which takes precedence over/etc/cron.deny
Common Command Description:
| Parameters |
Meaning |
Example |
| -L (Letter) |
View the contents of the crontab file. Hint: l can be understood as the abbreviation of list. |
crontab-l*** |
| -E |
Edit crontab file contents, hint: E can be understood as the abbreviation of edit. |
crontab-e*** |
| -I. |
Delete the contents of the Crontab file and prompt for confirmation before deleting it. Rarely used. |
Crontab-ri |
| -R |
Delete the contents of the crontab file with very little. |
Crontab-r |
| -U user |
Specifies the user that is used to perform the task. |
Crontab-u zdw-l |
| Special emphasis:-i,-r parameters in the production of little use, there is no need to use-e in the editor can. |
We use CRONTAB-E to enter the current user's worksheet edits, (crontab-e edit checks the syntax, and the direct Echo,vi edit does not check the syntax.) Each row is an edit command.
View/etc/crontab File
[[email protected] ~]# cat /etc/crontab shell=/ Bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# for details see man 4 crontabs# example of job definition:# ---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | |  &NBSP:---------- day of month (1 - 31) # | | |  &NBSP:------- month (1 - 12) OR jan,feb,mar,apr ...# |  |  |  | &NBSP---- day of week (0 - 6) ( SUNDAY=0 OR 7) OR sun,mon,tue,wed,thu,fri,sat# | | | | |# * * * * * user-name command to be executed
Explanation, the meaning of the time period in the CRONTAB syntax format is as follows:
| Paragraph |
Meaning |
Range of values |
| First paragraph |
Representative minutes |
00--59 |
| Second paragraph |
Representative hours |
00--23 |
| Third paragraph |
Representative Day, Day |
01--31 |
| Fourth paragraph |
Representative month |
01--12 |
| Fifth paragraph |
Representative Week, week |
0--7 (0 and 7 both represent Sunday) |
| Time Memory mouth: Day and week |
The special symbolic meanings in the crontab syntax format are as follows:
| Special symbols |
Meaning |
| * |
* number, indicating any time, is actually the meaning of "every" time. For example, if * * * cmd indicates that the CMD task is performed every day 23:00 each week. It is important to note that each time the * represents each, if the bit is * is the value range of the time on the bit, for example: * On the hour is equivalent to 00-23. Experience: Timed task rule if it is up to the hour, it is referred to daily. Needless to say, weeks. The * on the sub-position is equivalent to 0-59 |
| - |
The minus sign, which represents the delimiter, represents a time range, interval segment, and if 17-19 points, the daily 17,18,19 of 00 points performs the task. XX 17-19 * * * cmd. It is the meaning of the 17,18,19 point to be executed separately. |
| , |
A comma that indicates the meaning of the separator period. 17,18,19 * * * * * */bin/sh/scripts/zdw.sh 17, 18 and 19 points per day to execute the/scripts/zdw.sh script at the slightest moment. can also be used in conjunction with "-", for example, 3-5,17-19 * * */bin/sh/scripts/zdw.sh |
| /n |
n represents a number, that is, "every n units of time", for example: every 10 minutes a task can be written Into */10 * * * *, where the range of */10,* is 0-59, so it can also be written as 0-59/10. |
If we create a timed task but do not execute it, we need to see if the Crond service does not start, and if it does not start, it needs to be started before the scheduled task can be used.
To view and turn on timed tasks:
[Email protected] ~]#/etc/init.d/crond status #查看crond服务状态 crond is Stopped[[email prote CTED] ~]#/etc/init.d/crond restart #重启crond服务Starting crond: [O K
command example:
1.30 3,12 * * */bin/sh/scripts/zdw.sh
in this example, the first column is 30, which represents 30 minutes, the second column is 3, 12, which represents 3 points and 12 points, meaning of this timed task The /scripts/zdw.sh script is executed every day 3 o'clock in the morning and 12 o'clock noon (or described as daily 3:30 and noon 12:30).
2.30 */6 * * */bin/sh/scripts/zdw.sh
the first column, 30, represents 30 minutes, and the second row */6 represents every 6 hours, which is also equivalent to 6,12,18,24 's role.
This timed task means that the/scripts/zdw.sh Script task is executed at the slightest moment every 6 hours.
3.30 8-18/2 * * */bin/sh/scripts/zdw.sh
where the first column is 30, which represents 30 minutes, and the second columns 8- 12/2 represents every 2 hours between 8 o'clock in the morning and 18 o'clock in the afternoon, which is also equivalent to the role 8,10,12,14,16,18 alone lists.
So, this timed task means that every 2 hours between 18 o'clock in the afternoon and 8 o'clock in the morning is performed at 30 minute intervals. /scripts/ zdw.sh Script Task.
4.30 * * */application/apache/bin/apachectl Graceful
The above example shows that 21:30 restarts Apache per night.
5.45 4 1,10,22 * */application/apache/bin/apachectl Graceful
The above example shows the 1, 10, 22nd 4:45 restart of Apache.
6.10 1 * * 6,0/application/apache/bin/apachectl Graceful
The above example shows that Apache is restarted every Saturday, Sunday, 1:10.
7.0,30 18-23 * * */APPLICATION/APACHE/BIN/APACHECTL Graceful
The above example shows that Apache restarts every 30 minutes from 18:00 to 23:00 every day.
8.00 */1 * * */APPLICATION/APACHE/BIN/APACHECTL Graceful
The above example shows an hourly restart of Apache.
9.00 * 4 1-3/application/apache/bin/apachectl Graceful
The above example shows that the Apache is restarted from April to 11 o'clock in the morning every Monday to Wednesday.
emphasize: Week and day try not to use at the same time, otherwise may not achieve the desired effect.
Log storage location and view results:
[Email protected]/]# LL/VAR/LOG/CRON*-RW-------. 1 root root 34683 May 5 16:02/var/log/cron-rw-------. 1 root root 40839 Apr 03:31/var/log/cron-20160424-rw-------. 1 root root 50227 May 1 03:37/var/log/cron-20160501[[email protected]/]# tail-f/var/log/cronmay 5 15:01:01 localhost Run-parts (/etc/cron.hourly) [16907]: finished 0anacronMay 5 15:30:27 localhost crontab[16947]: (Root) BEGIN EDIT (root) Ma Y 5 15:31:28 localhost crontab[16947]: (Root) END EDIT (Root)
Learn from: Old boy
This article is from the blog "Write to Yourself", so be sure to keep this source http://zhaodongwei.blog.51cto.com/4233742/1770349
Linux Crond timed task service