Timed Task crontab Command

Source: Internet
Author: User
Tags mrtg

Excerpt from: http://www.centoscn.com/CentOS/help/2014/1030/4025.html

Objective

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, Crond reliability, robustness, everyone should be without a doubt.

Crontab easy to get started

Suppose I want to set a task, every minute to do a data synchronization, the path of this synchronization script is/home/blue/do/rsyncfile.sh, then I can configure, using blue User, in the terminal input

1    Crontab-e2    # This will go into the edit screen of VI and let you edit the work! Notice that each job is a row.  3    #分 time  Month week      full command line for |<============== task 4     *  * *  *  *       /home/blue/do/rsyncfile.sh

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!

If we need to modify the script to run the data synchronization every 5 minutes, then we also use CRONTAB-E to enter the editor:

1    */5 * * * *  /home/blue/do/rsyncfile.sh

If the server out of the problem, one day the data is not synchronized, so we need to fill the data, assuming that the script is/home/blue/do/rsyncfile_day.sh, but the peak period during the day, not many users at night, is low peak, we fill the data will occupy a large amount of bandwidth, Especially during the day, will affect the normal business, so generally we can let the data task to run at 2 o'clock in the morning, then also use Crontab-e into the editor:

1    0 2 1 4 *  /home/blue/do/rsyncfile_day.sh

In this way, we will start to start our data-filling script at 2:0 A.M. on April 1.

Synchronization of data, in the Internet company is a normal task, here you can see the charm of crontab, just want to write the simplest business logic, the dispatch to Crond do, completed a high reliability of a task, if you want to write this scheduler, I don't know how much energy it takes to be reliable and stable.

The syntax of crontab

1    crontab [-u username] [-l|-e|-R]2     options and Parameters:3  -u: Only root can perform this task, That is, to help other users create/ remove crontab work scheduling;4    -e  : Edit Crontab's work 5    -  L  : Check the work of Crontab 6-     R  : Remove all the crontab's work, and if you want to remove only one, use-e to edit

Query the user's current crontab content:

1    Crontab-L2*    /5 * * * *  /home/blue/do/rsyncfile.sh3    0214 *  /home/blue/do/rsyncfile_day.sh

Clear the user's current crontab:

1    Crontab-R2    crontab-l3for     Blue

If you want to delete a crontab task for the current user, use CRONTAB-E to enter the editor and delete the corresponding task.

Limitations of Crontab

/etc/cron.allow: will be able to use the Crontab account can be written to it, if not in this file users are not allowed to use crontab;

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

In order of priority,/etc/cron.allow than/etc/cron.deny priority, and judge 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!

/etc/crontab configuration file Explanation

"Crontab-e" is designed for users ' cron, and if it's a "routine task of the system", edit the/etc/crontab file.

That is crontab-e this crontab is actually/usr/bin/crontab this run file, but/etc/crontab is a "text file", must be the identity of root to edit this file.

First, we're going to take a look at Crontab's file.

 oncat/etc/crontab Geneva     Geneva#/etc/crontab:system-Wide crontabGeneva# Unlike any and crontab you don'T has to run the ' crontab' to# command to install theNewVersion when you edit Thisfile .# and filesinch/etc/CRON.D. These files also has username fields, -# That's none of the other Crontabs Do. ,      theshell=/bin/SHTenpath=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin One      A# m H Dom Mon Dow user command -     -* * * * Root CD/&& run-parts--report/etc/cron.hourly -     - 6* * * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.daily) the     - 6* *7Root Test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.weekly) -     the 6    1* * Root test-x/usr/sbin/anacron | | (CD/&& run-parts--report/etc/cron.monthly)

This file is almost exactly the same as what we have just released CRONTAB-E! Just a couple of different places.

1    Path= .... :

This is the search path to enter the run file! Using the default path configuration is enough!

1     * * * * *   root    CD/&& run-parts--report/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 6:25!

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:

1    */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:

1    */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!

The principle of crontab

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 Blue uses crontab, his work will be recorded in/var/spool/cron/blue! 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 is recorded in the/var/log/cron log file, so if your Linux does not know if there is a trojan, you can also search for/var/log/cron this login file!

The minimum detection limit for the Crond service is "minutes", so "cron reads the data contents of /etc/crontab and /var/spool/cron every Minute", so as soon as you finish editing/etc/ Crontab this file, and store it, then the cron configuration will automatically run!

Note: crontab under Linux will automatically help us re-read/etc/crontab routine work every minute, but for some reason or other Unix system, because crontab is read in memory, so after you modify/etc/cront AB, may not run immediately, this time please restart Crond this service! "/etc/init.d/crond restart" or "service Crond Restart"

Crontab's format explained

Each work (per line) is formatted with six fields, and the six fields have the following meanings:

Representative meaning Minutes Hours Date (days) 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 , which means "Sunday"! In addition, there are some auxiliary characters, presumably with these below:

Special characters Representative meaning
* (asterisk) means to accept at any moment! 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".
, (comma)

Represents the meaning of the separation period. For example, if the job to be released is 3:00 and 6:00, it will be:

0 3,6 * * command

The time parameter still has five columns, but the second column is 3,6, representing 3 and 6 are applicable!

-(minus)

Represents a period of time, for example, between 8 and 12 hours of 20 points per hour of work:

8-12 * * * command

Look carefully at the second column and turn it into 8-12. The meaning of 8,9,10,11,12 is applicable!

/n (Slash)

That n represents a number, meaning "Every n unit interval", for example, every five minutes:

*/5 * * * command

It's simple! with * and/5来 collocation, also can write 0-59/5, the same meaning!

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:

1 30 12 11 9 5 root echo"just test"<==这是错误的写法

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!

View crontab execution History under CentOS

Timed tasks were added to the crontab, but it was found that the desired results were not obtained, so it was suspected that Crontab did not perform the corresponding task, but how was the crontab executed?

This requires viewing the execution history of the crontab, as follows:

1    CD/var/Log2    tail- cron

You can see the related scheduled tasks that have been manipulated in the cron file.

Timed Task crontab Command

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.