1. Timed task comparison and cron syntax
Linux task scheduling can be divided into two categories:
- Tasks performed by the system itself
- Work performed by the user
|
Two other scheduled task software under Linux system:
At: For a scheduled task that executes only once, a service called ATD needs to be started Anacron: This command is mainly used for servers that are not available for anacron hours, and it is not possible to specify a specific time to perform tasks, but to perform tasks in days or after each boot of the system, it detects that the downtime should occur, but does not perform the crontab task, Execute it again. These two are less of a use |
Instruction Syntax:
Crontab [-u user] File Crontab [-u user] {-L |-r |-e} |
Instruction Description:
By crontab we can execute a specified system instruction or shell script at a fixed interval of time, which can be any combination of minutes, hours, days, months, weeks, and more (no combination of days and weeks). |
2. User Rights file
File |
Description |
/etc/cron.deny |
The user listed in this file is not allowed to use the crontab command |
/etc/cron.allow |
Users listed in this file are allowed to use the crontab command |
/var/spool/cron |
directory where all user crontab files are stored, named by user name |
3. Instruction option Description meaning table
Parameter name |
Meaning |
Specifying an example |
-L (Letter) |
Displays the contents of the user crontab file, |
Crontab-l |
-E |
Enter VI Edit user crontab file |
Crontab-e |
-I. |
Confirm prompt before deleting user crontab file |
Crontab-i |
-R |
Remove a user crontab file from the crontab directory |
Crontab-r |
-U user |
Specify consumer |
Crontab–u qinbf–l |
4. Format of Use of instructions
By default, the crontab file that the user sees is stored in the/var/spool/cron file, and its crontab corresponds to the user name.
The format is divided into seven paragraphs, the first five paragraphs are the time set segment, the sixth segment is the user who executes crontab (the default is the current user), and the seventh paragraph is the command segment to execute.
5. The meaning of the crontab time period is as follows:
Paragraph |
Meaning |
Range of values |
First paragraph |
Minutes |
00-59 |
Second paragraph |
Hours |
00-23 |
Third paragraph |
Date |
01-31 |
Fourth paragraph |
Month |
01-12 |
Fifth paragraph |
Day of the Week |
0-6 |
6. The meaning of the special symbol is as follows:
Special symbols |
Meaning |
* |
Indicates that any time is |
- |
A minus sign, representing a delimiter, representing a time range segment |
, |
A comma that indicates the meaning of the separated time period |
/n |
n represents the number, "that is, every n units of time" |
7. Crontab-Dependent services:
Chkconfig--list |grep Crond /etc/init.d/crond status |
8. Crontab Instance Description
1) 3,12 * * */bin/sh/scripts/oldboy.sh 2) */6 * * */bin/sh/scripts/oldboy.sh #--à second column */6 means every 6 hours, also equivalent to 6,12,18,24 3) 8-18/2 * * */bin/sh/scripts/oldboy.sh #-à the second column 8-18/2 represents every 2 hours between 8 o'clock in the morning and 18 o'clock in the afternoon, also equivalent to 8,10,12,14,16,18 4) * * * * * * * * * * * * * * * * * * */application/apachectl restart #----à effect is, night to the morning of 7, every hour of every minute to restart Apache, if you want to achieve an hourly restart, the previous minutes, you can set the 23-7/1 For 0 or 30 or something. 5) Server time synchronization: The system synchronizes the time every 5 minutes [Email protected] ~]# crontab-l */5 * * * */sbin/ntpdate time.windows.com >/dev/dull 2&>1 |
9. Crontab Order Production Environment professional wording
Example 1: Every minute prints its own name to/server/log/'s own name in the command's file. Answer: #print char task by Oldboy to programmer A at 2010-12-12 */1 * * * * echo "Oldboy" >>/server/log/oldboy.log 2>&1 Example 2: Every week Six 9:00 and 14:00 old boy Linux OPS Training Center learning (/server/script/oldboy.sh) Answer: #cron job for Ett by Oldboy 2010-12-12 XX 09,14 * * 6,0/bin/sh/server/script/oldboy.sh >/dev/null 2>&1 For the above two production examples illustrate: 1 Writing timed tasks It is a good habit to write down comments, such as WHO, what time, because who, did what? 2 The user can be omitted when executing a task, as far as possible with/bin/sh when executing the script, otherwise it may be unable to execute because the script does not have execute permission. 3 tasks that require root privileges can be logged into the root user and then set, if you do not need root privileges, you can log into the normal user, and then set up, here to pay attention to the environment variables of different users. 4 The end of the scheduled Task command is best added >/dev/null 2>&1 and other content, if you need to print the log, you can append to the specified log, not recommended to stay this unprofessional practice If the scheduled task does not add >/dev/null 2>&1 such as command settings, the time is long, may cause the message temporary directory/var/spool/clientmqueue file number soared, occupy a large number of disk space Inode node. 5 in the development of timed task programs or scripts, after debugging a good script, you should try to debug and screen output of the content command to remove, if necessary, can be directed to the log. |
Crontab Production Solution Case
Case 1:no space left on device fault 1
When setting crontab, no space left on device is indicated. With Df–k check there is still room, with Df–i Show/var has occupied 100%, if the inode is exhausted, the system will not be able to create files up and down.
Under/var/spool/clientmqueue/there are many files ls half-day did not respond, with RM-FR * will automatically jump out of root, and then use Xargs to cooperate with the solution. cd/var/spool/clientmqueue&& ls |xargs rm–f
Finally, the contents of the directory are cleaned up. Of course, if the number of files is very large, the execution of LS |xargs rm–f also long time unresponsive, this can also be directly cd/var/spool/&&rm–rf clientmqueue, and then mkdir Clientmqueque & & chmod 770 clientmqueue &&chown smmsp,smmsp-r/var/spool/clientmqueue.
Cause analysis: In the system, Cron executes the program has the output content, the output content will send to the cron user in the message form, but the SendMail did not start, therefore will produce these files.
Workaround: Turn on the mail service and add >/dev/null 2>&1 to the command in Crontab, directing the screen output to the log when making timed scripts.
Case 2:no space left on device fault 2
(1) When you create a new directory of File,system would say:no space left on device
[email protected]/var] #mkdir ett Mkdir:cannot Create directory ett ': No space left on device |
(2) When the Create crontab for account Ett,you would receive error information as follows;
| Crotab:installing New crontab Cron/tmp.6655:no space left in device Crontab:edits left in/tmp/crontab.6655 |
tr>
| Resolution 1 Check partition usage,df -H 2 check files under/var/lock, delete/var/lock/subsys/ Nfx,touch/var/ett, the file was successfully created, deleted/var/ett 3 After you delete any of the files under/var/lock, you can successfully create a file, and then the creation fails. 4 will check for additional directories that are expanded to/var/ 5 suspected partition/var/de inode exhaustion causes the above problems, check inode usage 6 partition/var/Not configured quota 7 Discovery/var/ Spool/clientmqueue There are too many files, because of the system's mail service configuration problems, resulting in the system sent messages piled up in the queue directory, occupy a large number of inode nodes. Processing method: Confirm that the file under/var/spool/clientmqueue is useless, delete it, if the number of files is too large to use rm-f * directly Delete, you can choose LS |xargs rm-f or delete the parent directory directly, then rebuild the directory and give the correct owner and permissions. The best solution is to reset the system message so that it can send it normally or find the source of the email, such as the crontab task for this article. Conclusion: The/var/of the partition is exhausted, the free inode is 0, resulting in the inability to create a new file or directory, and a similar problem in the future to be aware of using df -li to check inode usage. |
11. Production scenarios How to debug crontab timed tasks
- When debugging, the task execution frequency is adjusted a little, such as: every minute, every 5 minutes to execute;
- With the correct execution of the task time, after the setup is complete, you can modify the current time of the system;
- Add the log output to the script, then print the output to the specified log, and then observe the log contents to see if the execution is correct;
- NOTE: */1 * * * * echo "= =" >>/tmp/oldboy.log >/dev/null 2>&1 This covert task configuration that cannot be performed correctly;
- An absolute path is added to the file name after >> or > in the execution script.
- Note that the environment variable is caused by a timed task failure, such as when running a Java program, the environment variable to be re-export in the script.
- To view debug timed tasks through timed task logs
|
12. Timing Task Production Problem 8 Proverbs
- environment variable problem, typical example of a Java program running a case
- Task path problem, script to use absolute path
- Scripting permissions issues, preferably with/bin/bash, to prevent forgetting to give the script permission to execute the problem
- Time variable problem, the% number in crontab is considered to be newline, need to be escaped, crontab with "date +%y%m%d", must be replaced with "date +\%y\%m\%d", but written in the script is not necessary, This is one of the reasons why teachers recommend script files.
- >/dev/null 2>&1 Problems
- Timed Tasks plus comments
- Using a script instead of a command
- Avoid unnecessary program output
|
13. Regular cleanup of junk files
Crontab-l |tail-2 #del clientmqueue files by Oldboy at 2010-09-26 XX * * 0/bin/bash/server/scripts/del_sys_file.sh >/dev/null 2>&1 cat/server/scripts/del_sys_file.sh find/var/spool/clientmqueue/-type F | Xargs rm-f |
Crond Chapter Highlights
1) Users use CRONTAB-E custom scheduled task items, divided into six segments, [minutes, hours, days, months, weeks, directives].
2) The/etc/crontab Timing task of the system is divided into 7 segments, [Minute, Time, day, month, week, user, instruction].
3) Crontab Timing task of 5 production basic essentials and 7 debugging skills, especially learn to look at the service log to debug
4) 8-point proverbs to be noted in the production environment using crontab timing tasks
5) Attach importance to the case of production and learn to consolidate technical knowledge through case study
Linux Course Note Crond Introduction