Detailed explanation of the Crontab command for scheduled tasks

Source: Internet
Author: User
Tags mrtg crontab syntax

Detailed explanation of the Crontab command for scheduled tasks

Preface

Crontab is Unix and Linux commands used to set periodic execution. It is a very common technology on the Internet. Many tasks are set to run cyclically in crontab. If crontab is not used, the task is a resident program, this requires a high requirement on your program. One requires that your program be 24x7 without downtime, And the other requires that your scheduling program be reliable and in actual work, 90% of programs do not need to spend so much time and effort to solve the above two problems. They just need to write their own business logic and use crond to schedule the industrial-level program, there is no doubt about the reliability and robustness of crond.

Crontab Quick Start

Suppose I want to set up a task and perform a Data Synchronization every minute. The path of the synchronization script is/home/blue/do/rsyncfile. sh, so I can configure it in this way, use blue user, enter

1 crontab -e 2 # At this time, the vi editing screen will be displayed for you to edit the work! Note that each job is a row. 3 # Hour, day, month, week | <================= complete command line for the task 4 * * * * * /home/blue/ do /rsyncfile.sh

By default, any user who is not included in/etc/cron. deny can directly issue "crontab-e" to edit his or her routine commands! As mentioned above, the entire process will go to the vi editing screen and edit it with a work line. After editing, enter ": wq" to save and Leave vi!

If we need to change to the script that runs Data Synchronization every five minutes, we will also use crontab-e to edit the script:

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

If a problem occurs on the server and the data is not synchronized one day, we need to complete the data. Assume that the data population script is/home/blue/do/rsyncfile_day.sh, however, during the daytime hours, there are not many users at night, and it is low-peak hours. Our data population will occupy a large amount of bandwidth, especially during the daytime, which will affect normal business, therefore, we can run the data population task at a.m.. Similarly, we can use crontab-e to edit the task:

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

In this way, we will start the data population script at 02:00 on January 1, April 1.

Synchronizing data is a common task for Internet companies. Here you can see the charm of crontab. You only need to write the simplest business logic and hand over the scheduling to crond, A task with high reliability is completed. If you want to write such a scheduling program on your own, you don't know how much energy you need to spend to ensure reliability and stability.

Crontab syntax

1 crontab [-u username] [-l|-e|-r] 2 Options and parameters: 3 -U: only the root user can perform this task, that is, to help other users create/remove crontab Work schedule; 4 -E: Edit crontab Work content 5 -L: Query crontab Work content 6 -R: Remove all crontab To delete only one item, use-e to edit it.

Query the user's current crontab content:

1 crontab -l 2 */5 * * * * /home/blue/ do /rsyncfile.sh 3 0 2 1 4 * /home/blue/ do /rsyncfile_day.sh

Clear the user's current crontab:

1 crontab -r 2 crontab -l 3 no crontab for blue

If you want to delete a crontab task of the current user, use crontab-e to enter the editor and delete the corresponding task.

Crontab restrictions

/Etc/cron. allow: Write accounts that can use crontab to the account. If the account is not in this file, crontab cannot be used;

/Etc/cron. deny: You can use crontab to write accounts that cannot use crontab. If you have not logged the accounts in this file, you can use crontab.

/Etc/cron. allow ratio/etc/cron. deny should take precedence. However, we recommend that you keep only one of the above two files to limit your configuration! In general, the system retains/etc/cron. deny by default. You can write an account in/etc/cron. deny that does not want him to run crontab!

/Etc/crontab configuration file

"Crontab-e" is designed for the user's cron. If it is a "system routine task", You need to edit the/etc/crontab file.

That is, crontab-e is actually the run file of/usr/bin/crontab, but/etc/crontab is a "Text File 』, you must edit the file as root.

First, let's take a look at the contents of the crontab file.

01 cat /etc/ crontab 02 03 # /etc/crontab: system-wide crontab 04 # Unlike any other crontab you don't have to run the `crontab' 05 # command to install the new version when you edit this file 06 # and files in /etc/cron.d. These files also have username fields, 07 # that none of the other crontabs do. 08 09 SHELL=/bin/sh 10 PATH=/usr/ local /sbin:/usr/ local /bin:/sbin:/bin:/usr/sbin:/usr/bin 11 12 # m h dom mon dow user command 13 17 * * * * root cd / && run-parts --report /etc/ cron .hourly 14 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/ cron .daily ) 15 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/ cron .weekly ) 16 52 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 just issued crontab-e! Only a few places are not the same

1 PATH=....:

Enter the search path of the running file! The default path configuration is enough!

1 17 * * * * root cd / && run-parts --report /etc/ cron .hourly:

In this/etc/crontab, four tasks are pre-configured, which are performed every hour, every day, every week, and every month! But what follows the five columns is not a command, but a new column, that is, what is the "Identity of the command strings following! This is different from the user's crontab-e. The identity does not need to be specified for the user's crontab, but the identity must be specified in/etc/crontab! For the content of the preceding table, the system performs routine work as root by default.

So what are the subsequent commands? You can use "which run-parts" to search for it. It is actually a bash script! If you directly go to/usr/bin/run-parts and check it, you will find that this command will catch all the files in the "directory" that are followed by it and run it! That is to say, "If you want the system to take the initiative to help you run a command every hour, write the command as a script and place the file in/etc/cron. hourly/directory!

Now, do you know how the system schedules a bunch of routine jobs by default? If you issue "ll/etc/cron. daily", you can see a bunch of files. These files are the scripts provided by the system, and these scripts will start running at every day!

Suppose you want to create a directory so that the system can run all the files in the directory every 2 minutes. You can write down the following line in/etc/crontab:

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

Of course, the/etc/cron. min directory must exist! What if I want to run a program without a directory? What should I do? For example, when we want to detect and analyze network traffic every five minutes, we can write as follows:

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

How! It's easy to create routine commands! If you are a system administrator and your work is a routine task of system maintenance, you can directly modify the file/etc/crontab! Convenient and easy to manage!

Crontab principles

After you use the crontab command to create a Job Schedule, the job will be recorded in/var/spool/cron/and identified by an account! For example, after blue uses crontab, his work will be recorded in/var/spool/cron/blue! However, please note that do not use vi to directly edit the file, because the input syntax may be incorrect, leading to the failure to run cron! In addition, every task running cron is recorded in the/var/log/cron logon file. Therefore, if your Linux system does not know whether a trojan is implanted, you can also search for the/var/log/cron logon file!

The minimum detection limit of the crond service is "Minutes", so "cron reads data in/etc/crontab and/var/spool/cron once per minute". Therefore, after you edit the/etc/crontab file and save it, the cron configuration will automatically run!

Note: crontab under Linux will automatically help us read the routine work items of/etc/crontab every minute, but for some reason or other Unix systems, since crontab is read to the memory, it may not run immediately after you modify/etc/crontab. Please restart the crond service at this time! "/Etc/init. d/crond restart" or "servicecrond restart 』

Crontab format

Each job (each line) is formatted with six columns. The meaning of these six columns is as follows:

Meaning minute Hour date (day) month week command number range: 0-590-231-311-120-7 ah just command ah

What's interesting is the week! When the number of weeks is 0 or 7, they all mean "Sunday! In addition, there are some auxiliary characters, which are roughly the following:

Special characters represent meaning * (asterisks) represents meaning accepted at any time! For example, if the day, month, and week in Example 1 are all *, it means that "Run Subsequent commands at of every day of the month or day !, (Comma)

It indicates the meaning of the separated time periods. For example, if you want to release a job at and, it will be:

0, 6 *** command

The Time Parameter still has five columns, but the second column is 3, 6, which indicates that both 3 and 6 are applicable!

-(Minus sign)

For example, during a period of time, every hour between and has a job:

20 8-12 *** command

The second column is changed to 8-12! It indicates that 8, 9, 10, 11, and 12 are applicable!

/N (diagonal line)

That n represents the number, that is, the meaning of "every n units interval", for example, once every five minutes, then:

*/5 * command

Easy! Use * And/5 together, or enter 0-59/5!

The Week and the day and month cannot coexist at the same time.

Another thing to note is: "You can use weeks or days or months as cycles, but you cannot use the" months and months "Mode 』. This means that you cannot write a Job Schedule like this:

1 30 12 11 9 5 root echo "just test" <= This is an incorrect statement.

Originally, you thought that this work would only be performed on Friday, but the system may decide to do this once every Friday, or do it separately on May 15, every year, this is different from your original plan ~ So you have to pay attention to this place! The above statement is incorrect!

View crontab execution history in CentOS

A scheduled task is added to crontab, but the expected result is not displayed. Therefore, it is suspected that crontab does not execute the corresponding task. How can I locate whether crontab is executed?

In this case, you need to view the execution history of crontab. The specific location is as follows:

1 cd /var/log 2 tail -100 cron

You can view related scheduled tasks in the cron file.

References:

Http://vbird.dic.ksu.edu.tw/linux_basic/0430cron_3.php

Http://baike.baidu.com/view/1229061.htm

1. Crontab format description

We can use crontab-e to add the command to be executed. The command execution result, whether it is standard output or error output, will be sent to the user in the form of mail.

The added command must be in the following format:

* ***/Command path

The first five fields can take an integer to specify when to start working. The sixth field is a string, that is, a command field, which includes the crontab command for scheduling and execution. The fields are separated by spaces and tabs.

The first five fields are represented respectively:

Minute: 0-59
Hours: 1-23
Date: 1-31
Month: 1-12
Week: 0-6 (0 indicates Sunday)

You can also use some special symbols:

*: Indicates any time
,: Indicates Segmentation
-: Indicates a segment. For example, 1-5 in the second end indicates.
/N: indicates that each n is executed once. For example, in the second segment, */1 indicates that the command is executed every one hour. You can also enter 1-23/1.

Some examples:

00, 8, 12, 16 */data/app/scripts/monitor/df. sh
30 2 ***/data/app/scripts/hotbackup/hot_database_backup.sh
10 8, 12, 16 */data/app/scripts/monitor/check_ind_unusable.sh
10 8, 12, 16 */data/app/scripts/monitor/check_maxfilesize.sh
10 8, 12, 16 */data/app/scripts/monitor/check_objectsize.sh

43 21 *** run
15 05 *** run
0 17 *** execute
0 17 ** 1 run at every Monday
17 **, 3 run at and on every Sunday, Tuesday, and Wednesday.
0-10 17 1 ** every 1 minute from to on the first day of the month
0 0*1 on the 1st and 15th of the month
42 4 1 ** executed at on the first day of the month
0 21 ** 1-6 from Monday to Saturday
0, 10, 20, 30, 40, 50 *** run every 10 minutes
*/10 * execute every 10 minutes
* 1 *** run every 1 minute from
0 1 *** execution
0 */1 * execution at every 1 hour
0 * execution at every 1 hour
2 8-20/3 *** run
30 5, ** executed at on the 1st and 15th

Ii. & background Command Execution

When running a job on the foreground, the terminal is occupied by the job, but it does not occupy the terminal when running the job on the background. You can run the job in the background using the & command.

For example:

1 30 2 * * * /data/app/scripts/hotbackup/hot_database_backup.sh &

Be careful when running jobs in the background: do not execute commands that require user interaction in the background, because your machine will be silly there.

However, running a job in the background will output the result to the screen, interfering with your work. If a job running in the background produces a large amount of output, you 'd better use the following method to redirect its output to a file:

For example:

1 command >out. file 2>&1 &

In this example, 2> & 1 indicates that all standard output and error output will be redirected to an out. file.

3. 2> & 1 meaning

Let's take a look at an example:

1 0 2 * * * /u01/ test .sh >/dev/null 2>&1 &

This statement means to execute this command in the background, redirect error output 2 to standard output 1, and then put all the standard output 1 to the/dev/null file, that is, empty.

Here are several numbers:

0 indicates keyboard input
1 indicates standard output
2 indicates error output

We can also write:

0 2 ***/u01/test. sh 1>/u01/out. file &
0 2 ***/u01/test. sh 2>/u01/out. file &
0 2 ***/u01/test. sh 2>/u01/out. file 2> & 1 &

Redirect the tesh. sh command output to out. file, that is, the output content is not printed to the screen, but output to the out. file.

2> & 1 redirects the error output to the standard output. Then, redirect the standard input to the file out. file.

& 1 indicates the description 1 of the file, indicating the standard output. If & is missing, it becomes the number 1, indicating redirection to file 1.

&: Background execution

Test:

Ls 2> 1: No errors without 2 files are reported, but an empty file 1 is output;
Ls xxx 2> 1: no error in xxx file output to 1;
Ls xxx 2> & 1: The file 1 is not generated, but the error is returned to the standard output;
Ls xxx> out.txt 2> & 1 = ls xxx 1> out.txt 2> & 1: Because the redirection symbol> expiration is 1, the error output and standard output are all transmitted to the out.txt file.

4. 2> the reason why & 1 is written below

Format: command> file 2> & 1 = command 1> file 2> & 1

First, the command> file redirects the standard output to the file. 2> & 1 indicates that the standard output is copied due to a standard error, that is, it is also redirected to the file, the final result is that both the standard output and errors are redirected to the file.

If changed to: command 2> & 1> file

2> & 1 the standard error copies the standard output, but the standard output is still on the terminal.> The output is redirected to the file, but the standard error is still on the terminal.

Additional reading:

Shell standard output, standard error>/dev/null 2> & 1

How to run Linux scheduled task crond in seconds (for example, every 3 seconds)

Lessons learned:

Execute the Python script at every night on the server to back up the MySql database. The command is as follows:

1 * 23 * * * python / var /www/html/crontab_python/back_db.py >/dev/null 2>&1

As a result, 60 backup files are generated for each backup. Check the timing Task Command carefully and find that the "0" is missing in the "points" position ", because "*" indicates any value at this position, the modification is as follows:

1 0 23 * * * python /var/www/html/crontab_python/back_db.py >/dev/null 2>&1

0 4 ***/usr/local/php/bin/php/usr/local/nginx/www/backup-db/backup_db.php 172.16.8.26>/dev/null 2> & 1
0 4 ***/usr/local/php/bin/php/usr/local/nginx/www/backup-db/backup_db.php 172.16.10.151>/dev/null 2> & 1

Related Article

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.