Linux routine work scheduling (crontab)

Source: Internet
Author: User
Tags mrtg

Crontab is UNIX and Linux used to set the periodic execution of instructions, is the Internet is very common technology, a lot of tasks will be set in the crontab loop execution, if not use crontab, then the task is a resident program, which is higher for your program requirements, A request your program is 24x7, one is to require your scheduler is more reliable, in the actual work, 90% of the program is not necessary to spend so much time and energy to solve the two problems above, only need to write their own business logic, through the Crond this industrial-level program to dispatch on the line, There is no doubt about the reliability and robustness of Crond.

User's configuration

The user wants to create a circular work schedule, using the crontab command ~ However, for security purposes, we can restrict the use of crontab user account Oh! The restriction data used are:

    • /etc/cron.allow:
      will be able to use Crontab's account to write to it, if not in this file users can not use crontab;

    • /etc/cron.deny:
      You will not be able to use Crontab's account to write to it, if not recorded in this file users, you can use the crontab.

It looks like it! Similarly, in order of priority,/etc/cron.allow than/etc/cron.deny priority, and judging above, these two files only choose one to limit it, therefore, it is recommended that you just keep one, so as not to affect their configuration above the judgment! In general, the system is reserved by default/etc/cron.deny, you can not want to let him run crontab of the user to write/etc/cron.deny, an account line!

When the user uses the crontab command to create a work schedule, the work is recorded in the/var/spool/cron/, and the account is used as a discriminant! For example, after Dmtsai uses crontab, his work will be recorded in/var/spool/cron/dmtsai! Note, however, that you do not use VI to edit the file directly, because you may not be able to run cron due to input syntax errors! In addition, every job that cron runs will be logged to/var/log/cron, so if your Linux does not know if there is a trojan, you can also search for/var/log/cron this log-in file!

Well, then let's talk about Crontab's grammar!

crontab [-u username] [-l|-e|-r] options and parameters:  -u: Only root can do this task, that is, to help other users create/remove crontab work scheduling;-e  : Edit crontab Work Content-L  : Check the work of Crontab-R  : Remove all the crontab's work, and if you want to remove only one item, use-E to edit it. crontab-e# At this time will enter the VI editing screen let you edit the work! Notice that each job is a row. 0 * * * mail Dmtsai-s "at" </HOME/DMTSAI/.BASHRC#分 |<============== command string ================= =======>|    

By default, any user who is not included in the/etc/cron.deny will be able to directly release "Crontab-e" to edit his or her own routine commands! The whole process as mentioned above, will go into the edit screen of VI, and then a work line to edit, after editing the input ": Wq" after the storage Left VI can be! Each work (per line) is formatted with six fields, and the six fields have the following meanings:

Representative meaning Minutes Hours Date Month Week Command
Number Range 0-59 0-23 1-31 1-12 0-7 Yes, order it.

The interesting thing is that "Week" Oh! The number of weeks is 0 or 7 o'clock, which means "Sunday"! In addition, there are some auxiliary characters, presumably with these below:

special character Span style= "color: #ffffff;" > meaning
* (asterisk) stands for any time accepted meaning! For example, the day, month, and week of an example are all *, which means "running a follow-up command at 12:00 on the day of the week, regardless of the month".
represents the meaning of the separator period. For example, if the job to be released is 3:00 and 6:00, it will be:
0 3,6 * * * command
time parameter or five column, no The second column is 3,6, which means 3 and 6 are applicable!
-(minus sign) for a period of time, for example, 8 to 12 hours of 20 points per hour to do a job:
8-12 * * * command
Look at the second column to become 8-12! The meaning of 8,9,10,11,12 is applicable!
/n (slash) that n represents a number, that is, "every n unit interval" meaning, for example, every five minutes, then:
*/5 * * * * command
very simple! with * and/5来 collocation, also can write 0-59/5, the same meaning!

Let's take a couple of examples to practice the look! The case below please actually use Dmtsai this identity as a look! The follow-up action will be able to match up!

Example: If your girlfriend's birthday is May 2, you want to send him a letter on the May 1 of 23:59, the letter is written in/home/dmtsai/lover.txt, how to do? A: After the direct release of Crontab-e, the editor becomes:
1 5 * Mail Kiki that way, every year Kiki will receive this letter from you. Oh! (Of course, the contents of the letter will change every year!) )

Example: What if you need to run/home/dmtsai/test.sh once every five minutes? A: Also use Crontab-e to enter edit:
*/5 *
* * * */home/dmtsai/test.sh

That crontab everyone has only one file exists, is in/var/spool/cron inside Ah! There is also a suggestion: " when the command is released, it is best to use the absolute path, so the comparison will not find the running file Oh!" "

Example: If you have a meeting with friends every Saturday, then you want to tell your friends about Saturday dating every Friday 4:30 do not forget: Answer: or use crontab-e Ah! *
* 5 Mail [email protected]

It's really simple! Oh! So, how to query the user's current crontab content? We can look at it this way:

CRONTAB-L59 1 5 * Mail Kiki </HOME/DMTSAI/LOVER.TXT*/5 * * * */home/dmtsai/test.sh30 * * 5 mail [email protect ed] crontab-lno crontab for Dmtsai

Did you see it? Crontab "The whole content is gone! "So please note:" If you just want to delete a crontab work item, please use CRONTAB-E to edit it again! "If you use the-r parameter, all the crontab data contents will be deleted!" Don't be too careful!

Configuration file for the system:/etc/crontab

This "CRONTAB-E" is designed for users ' cron, what to do if it is a "routine task of the system"? Do you still need to crontab-e to manage your routine work schedule? Of course not, you just have to edit /etc/crontab this file! There is a need to pay special attention to Oh! That is crontab-e this crontab is actually/usr/bin/crontab this run file, but/etc/crontab is a "plain text file" Oh! You can edit this file as root!

Basically, the minimum detection limit for the cron service is "minutes", so "cron reads the contents of/etc/crontab and/var/spool/cron every minute", so as soon as you finish editing the/etc/crontab file, and After storing it, the cron configuration will automatically run!

Tips:
The crontab under Linux automatically helps us re-read the/etc/crontab routine work every minute, but for some reason or another Unix system, crontab is read into memory, so after you modify/etc/crontab May not run immediately after this, please restart Crond this service! "/etc/init.d/crond Restart"

Talk less, let's take a look at this/etc/crontab content!

 [[email protected] ~]# cat/ Etc/crontabshell=/bin/bash <== what kind of shell interface to use path=/sbin:/bin:/usr/sbin:/usr/b In <== Run File search path mailto=root <== if extra stdout to EM Ail send the data to who home=/<== default this shell's home directory is located # run-parts01 * * * * roo T run-parts/etc/cron.hourly <== 4 per hour * * * Root run-parts/etc/cron.daily <== 4 * * 0 root run-parts/etc/cron.weekly <== every Sunday 4 1 * * Root run-parts /etc/cron.monthly <== 1th per month  time-of-day weekly runner identity command string    /span>       

You probably know the contents of this file. Oh, yes! This file is almost exactly the same as what we have just released CRONTAB-E! Just a few places are not quite the same:

  • Mailto=root:

    This project is to say, when the/etc/crontab of the routine work in this file is wrong, or if the result of the work is Stdout/stderr, who will the error message or the screen display message be passed to? By default, the system sends a mail message to root directly! However, because root and can not be in the client to POP3 such as the software to receive the letter, therefore, bird brother usually will this e-mail to their account, so I always understand the status of the system! For example: [email protected]

  • Path= .... :

    Remember the running path problem we've been talking about in the 11th chapter of BASH! That's right! This is the search path to enter the run file! Using the default path configuration is enough!

  • * * * * * Root run-parts/etc/cron.hourly:

    The/etc/crontab is preconfigured with four tasks, each hourly, daily, weekly, and monthly. However, after five fields, not the command, but a new field, that is, " run the command behind the identity" why! This is not the same as the crontab-e of the user. Because the user's own crontab does not need to specify the identity, but/etc/crontab inside of course to specify the identity! In the table above, the system default routine work is done as root.

    So what's the order behind that? You can use "which run-parts" search to see, actually that is a bash script! If you go straight to the/usr/bin/run-parts, you will find that this command will catch all the files in the "directory" behind it and run it! This means " if you want the system to actively help you run a command every hour, write the command into a script and place the file in the/etc/cron.hourly/directory"!

    Now do you know how the system is doing his default pile of routine work scheduling? If you give "ll/etc/cron.daily" you can see a bunch of files, those files are the script provided by the system, and the heap of scripts will start every day 4:02! This is why if you are a night cat, you will find it strange that the Linux system will be busy at the beginning of 4:02 to make some hard drive running sound! Because he had to do Makewhatis, UpdateDB, RPM rebuild and so on!

This script is supported by the run-parts provided by CentOS, so/etc/crontab supports two ways of issuing commands, one for direct commands and one for directory planning, for example:

    • Command patterns
      * * * * * Dmtsai mail-s "testing" Kiki
      Run the Mail command once per hour as the user of Dmtsai.

    • Directory Planning
      */5 * * * * Root Run-parts/root/runcron
      Create a directory of/root/runcron that will be written to this directory every five minutes of "running files", allowing the system to run every five minutes of all available files in that directory.

Good! You probably know this one now! Ok! Suppose you now want to make a directory that allows the system to run all the files that can be run in this directory every 2 minutes, and you can write down the following line in/etc/crontab:

*/2 * * * *
root run-parts/etc/cron.min

Of course,/etc/cron.min This directory is a need to exist Oh! So what if I need to run a "program" that doesn't need a directory? What should be good? For example, when detecting network traffic, we want to detect the analysis every five minutes, so we can write:

*/5
* * * * root/bin/mrtg/etc/mrtg/mrtg.cfg

How is it! Creating routine commands is simple! If you are a system administrator and your work is a routine task in system maintenance, modify the/etc/crontab file directly. and convenient, and easy to manage it!

Some precautions

Sometimes, when we create a routine work with a system cron, we should pay attention to some aspects of the use of the features. For example, if we have four jobs that are going to be done five minutes at a time, are all the four actions all at the same point? If it is done at the same time, the four actions are very resource-intensive, so that every five minutes will not make the system busy? Oh! At this time good allocation of some running time is OK! So, take a look:

    • The problem of uneven allocation of resources

When a large number of use of crontab, there will always be problems, the most serious problem is "system resource allocation uneven" problem, to Bird's system for example, I have to detect the host traffic information, including:

    • Flow
    • Traffic detection for other PCs in the region
    • CPU usage
    • RAM Utilization
    • Online people instant detection

If each process is started at the same time, then at some point, my system will become quite busy, so, this time must be configured separately! I can do this:

VI/ETC/CRONTAB1,6,11,16,21,26,31,36,41,46,51,56 * * * * Root  cmd12,7,12,17,22,27,32,37,42,47,52,57 * * * * Root  CMD23,8,13,18,23,28,33,38,43,48,53,58 * * * * Root  cmd34,9,14,19,24,29,34,39,44,49,54,59 * * * * Root  CMD4

Did you see it? When the "," is separated, please note that there are no whitespace bytes! (continuous meaning) in this way, the process of working every five minutes can be done at different times. You can make the system run more smoothly yo!

    • Cancel an output item that you don't want

Another problem is that "when there is output data in a running project or a run, the data will be sent to the MAILTO configured account", OK, then when there is a scheduling error (such as DNS detection system, if the DNS on a primary host hangs off, Then you will always receive the error message! Do Oh! Remember the data flow redirection you talked about in Chapter 11? Direct "command redirection" to output output to /dev/null this garbage can!

    • Safety inspection

Many times the Trojan is implanted in a routine order, so you can check the contents of the/var/log/cron to see if there is a "cron that is not your configuration running?" "You need to be careful at this point!"

    • The week and the sun cannot coexist simultaneously

Another area to be aware of: "You can cycle in weeks or days, but you can't use the" months and days of the Week "mode to work. This means that you can't write a schedule like this:

9 5 root echo "Just test"   <== This is the wrong wording.

Originally you think September 11 and for Friday will carry out this work, helpless is, the system may decide each Friday to do, or each year September 11 respectively, so that your original plan is not the same ~ so Luo, you have to pay attention to this place! The above notation is wrong!

Reprinted from: http://vbird.dic.ksu.edu.tw/linux_basic/0430cron_3.php

Linux routine work scheduling (crontab)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.