Cron analysis of timed tasks in Rhel

Source: Internet
Author: User

Cron

Cron is a tool name that executes the script you specify according to a certain time policy, just as it does with timed tasks under Windows.

Its corresponding daemon is Crond, which is started as a service at boot time.

Therefore, Cron can be managed using service management commands, such as Service,chkconfig.

For specific use, refer to:

http://blog.csdn.net/jx_jy/article/details/13289217

Configuration file

The crond process wakes up once every minute, reads into its own configuration file, executes the script task according to the policy set in the configuration file, and the Cron profile has two locations

/etc/crontab

/var/spool/cron

First look at the contents of the/etc/crontab file

shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# RUN-PARTS01 * * * * Root run-parts/etc/ CRON.HOURLY02 4 * * * Root Run-parts/etc/cron.daily22 4 * 0 Root run-parts/etc/cron.weekly42 4 1 * * Root run-parts/e Tc/cron.monthly
The first four lines of the variable definition section will not say, specify Path,shell and so on.

The last four lines are the tasks to be performed and the policies that perform the task.

The specific format is

1, Sub (1-60) * means all, for example, 2-10 means only the 2nd to 10th minutes per hour to perform the task, you can also use the semicolon 2-10/2, the following 2 means the interval of two minutes to execute once, if you want to interval 3 minutes, 2 to 3, the following columns of the time, day, month, The week's formatting is consistent with this syntax.

2, when (1-24)

3, Day (1-30) here each month of the day is not the same as the natural month, there are 31 days 28 days or something, its range is 1-30.

4, Month (1-12)

5, week (0-6) 0 means Sunday

6. The user who executed the script

7. Execution options If you need to execute all the scripts under a folder, you need to use the Run-parts parameter, and if you specify a specific script later, this place can be empty.

8. The script to execute, or the directory where the script is located

As you can see from the above, the system has predefined paths under the specified path, such as/etc/cron.hourly the script below is executed at the first minute of each hour. The script under/etc/daily is executed at 4:02 every day.

So, if you want to add your own scheduled tasks, you can add a line directly to the crontab file, or you can follow the system booking strategy and put your script under the appropriate folder.

And look at another configuration file.

/etc/spool/cron/

In this directory, each user who now has a scheduled task has a file named after it, and we open a file to look at the content.

[Email protected] cron]# Pwd/var/spool/cron
[[email protected] cron]# ll total 8-rw-------1 root root 12-14 13:34 oracle-rw-------1 root root 12-14 13:33 root
[email protected] cron]# Cat ROOT18-23/2 * * * */home/oracle/shell/test_cron.sh

We can look at the contents of the file under the last root user.

Comparing the contents of the crontab file above, we find that it is only the part of the specified user is missing, parsing the above scheduled task as

/home/oracle/shell/test_cron.sh scripts are executed every 2 minutes between 18 and 23 minutes per hour.

Here I did a small not much significance of the experiment, that is, the script under the same folder in the execution order of how to make, the experimental results show that the execution results are consistent with the LS view, perhaps this is the RC0N.D directory of the script is the beginning of the number of the order to determine the principle. If you have multiple scripts that have dependencies, it is best to use a script call to resolve them. Cron Configuration Command crontab

The Crontab command is a dedicated command that you configure for timed tasks, except that the root user, such as Oracle,/etc/crontab these profiles without changing permissions, or even/var/spool/cron directories.

So you need to use the crontab command, which is a simple command.

-U Oracle followed by a user name, which indicates a configuration query for a specific user, and if not, a configuration query for the current user

-L Show Scheduled tasks under User

-e editing user's timing task, in fact, is to open the VI editor, adding and deleting the corresponding entries

-R Delete all scheduled tasks for the user

For example: View scheduled Tasks under Oracle Crontab-u oracle-l

Edit Scheduled Tasks under Oracle Crontab-u ORACLE-E

Remove all scheduled tasks for Oracle Crontab-r

In addition, this crontab command only queries the contents of the user file in the/var/spool/cron directory, not the contents of the/etc/crontab. Therefore, it is necessary to check the user's scheduled tasks carefully.

Cron Auxiliary Tool Anacrond

As you can see from the cron time strategy, it performs timed tasks at a specific point in time, such as 4:02 A.M. if your machine is not powered up 24 hours or just 4 to 5. Then this day's scheduled task will not be able to execute, so, Anacron is to make up for this weakness of cron.

In fact, Anacron has only one configuration file/etc/anacrontab, first we look at its contents

[Email protected] etc]# pwd/etc
[email protected] etc]# Cat Anacrontab #/etc/anacrontab:configuration file for anacron# see Anacron (8) and Anacrontab (5 ) for details. SHELL=/BIN/SHPATH=/SBIN:/BIN:/USR/SBIN:/USR/BINMAILTO=ROOT1       65cron.dailyrun-parts/etc/ Cron.daily770cron.weeklyrun-parts/etc/cron.weekly3075cron.monthlyrun-parts/etc/cron.monthly
The first few lines do not say, we look at the last 3 lines. or by the column description

1, the interval, the unit is the day

2, delay execution time, unit is minutes

3, the implementation of the task of marking, is a name, the custom

4, the execution of the script, this place is still using run-parts, it is obvious that this is the execution of all the contents of this folder, like Cron

So how does it work?

First, it establishes and maintains a file in the/var/spool/anacron/directory for each row of tasks, which stores the timestamp of the last execution of the scheduled task, and the content class as

[email protected] anacron]# cat cron.daily 20141214
is a date. After my tests, the time in this file was incorrectly maintained by the Crond and Anacrond two processes, and it was possible to Crond Anacrond and then anacrond to maintain it each time it was executed, in short, If Crond or Anacrond executes the contents of the cron.daily directory, the timestamp in the file will be updated.

And, it is updated before the execution plan starts, so if you have not finished all the scripts to perform the shutdown operation, the boot after the time stamp has been updated.

So, the whole execution process is probably like this.

After booting, the Anacrond process looks at the event stamp that exists in the/var/spool/anacron/directory, comparing the configuration policy in/etc/anacrontab, such as the first one, if the current time is more than 1 days past the recorded event in the file. Then the Anacrond process will perform the subsequent scheduled task and update the timestamp file after 65 minutes.



Cron analysis of timed tasks in Rhel

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.