Cron parsing of scheduled tasks in rhel

Source: Internet
Author: User

Cron parsing of scheduled tasks in rhel
Cron

Cron is a tool name. Its function is to execute a specified script based on a certain time policy, which is the same as a scheduled task in windows.

The corresponding daemon is crond, Which is started when the service is started.

Therefore, cron can be managed using service management commands, such as service and chkconfig.

For more information, see:

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

Configuration File

The crond process is awakened once every minute. It reads its configuration file and runs the script task according to the policy set in the configuration file. The cron configuration file has two locations.

/Etc/crontab

/Var/spool/cron

First, check the content 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 /etc/cron.monthly
The definition of the variables in the first four rows will not be mentioned. Specify PATH, SHELL, and so on.

 

The last four rows are the task to be executed and the policy to execute the task.

The specific format is

1. Minute (1-60) * indicates all. For example, 2-10 indicates that the task is executed only after 2nd to 10th minutes per hour. You can also use semicolons (;) 2-10/2, 2 indicates that the execution is performed every two minutes. If you want to interval 3 minutes, change 2 to 3. The time, day, and month of the following column are displayed, the specified format of the week complies with this syntax.

2. Hour (1-24)

3. Days (1-30) Here, the day of each month is not the same as that of a calendar month. There are 31 days and 28 days, and the range is 1-30.

4. Month (1-12)

5. Week (0-6) 0 indicates Sunday

6. the user who executes the script

7. run the option. to execute all the scripts in a folder, use the run-parts parameter. If a specific script is specified later, this field can be blank.

8. The script to be executed or the directory where the script is located

As you can see from the above, the system predefines some paths under the specified path, for example, the script under/etc/cron. hourly will be executed once in the first minute of every hour. Scripts in/etc/daily are executed at 04:02 every day.

Therefore, if you want to add your own scheduled tasks, you can also add a row to the crontab file or place your scripts in the appropriate folder according to the system's predefined policy.

Let's look at another configuration file.

/Etc/spool/cron/

In this directory, every user who has a scheduled task now has a file named after him. Let's open a file and check the content.

 

[root@server1 cron]# pwd/var/spool/cron
[Root @ server1 cron] # ll total 8-rw ------- 1 root 48 12-14 13:34 oracle-rw ------- 1 root 48 12-14 13:33
[root@server1 cron]# cat root18-23/2 * * * * /home/oracle/shell/test_cron.sh

 

We can check the file content of the last root user.

Compared with the content of the crontab file above, we found that it is only missing the part of the specified user, parse the above scheduled task

Execute the/home/oracle/shell/test_cron.sh script every 18 to 23 minutes every hour.

Here I did a small experiment that doesn't make much sense, that is, how to change the execution sequence of scripts in the same folder, the experimental results show that the execution results are consistent with the ls viewing sequence, which may be rc0n. all scripts in the d directory start with a number to determine the sequence. If multiple scripts are dependent, it is best to use the script to call the solution. Crontab

The crontab command is a dedicated Command for you to configure scheduled tasks. Except for the root user, for example, oracle users do not have permission to change the configuration files such as/etc/crontab, even the/var/spool/cron directory cannot be accessed.

Therefore, you need to use the crontab command. This command is very simple.

-Add a user name after u oracle to query the configuration of a specific user. If this parameter is not provided, query the configuration of the current user.

-L Display User scheduled tasks

-E. to edit a user's scheduled task, open the vi editor and add, delete, and modify corresponding entries.

-R: deletes all scheduled tasks of a user.

For example, view the scheduled task crontab-u oracle-l in oracle.

Edit the scheduled task crontab-u oracle-e in oracle

Delete all scheduled oracle task crontab-r

In addition, this crontab command only queries the content of the user file in the/var/spool/cron directory, but does not query the content of/etc/crontab. Therefore, you need to check your scheduled tasks in detail.

Cron's auxiliary tool anacrond

It can be seen from the cron time policy that the scheduled task is executed at a specific time point, for example, 04:02, if your machine is not started 24 hours a day, or shut down at. Then the scheduled task of the day cannot be executed, so anacron is designed to make up for this weakness of cron.

In fact, anacron only has one configuration file/etc/anacrontab. First, let's take a look at its content.

 

[root@server1 etc]# pwd/etc
[root@server1 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
We won't talk about the first few lines. Let's look at the last three lines. Or by Column

 

1. interval, in days

2. delayed execution time, in minutes

3. The execution task id is a custom name.

4. run-parts is used in the executed script. Obviously, this is to execute all the content in this folder, just like cron.

So how does it work.

First, it creates and maintains a file for each row of tasks in the/var/spool/anacron/directory. This file stores the last timestamp of the scheduled task, content class such

 

[root@server1 anacron]# cat cron.daily 20141214
Is a date. After my tests, the time errors in this file are jointly maintained by the crond and anacrond processes, or the crond may notify anacrond each time it is executed and then maintain it through anacrond. In short, if crond or anacrond executes cron. the content of the daily directory, and the timestamp in this file will be updated.

 

In addition, it is updated before the execution plan starts. Therefore, if you have not finished executing all the scripts, the shutdown operation will be executed, the timestamp is updated after the instance is started.

Therefore, the entire execution process is like this.

After the startup, The anacrond process will check the event timestamp in the/var/spool/anacron/directory and compare the configuration policies in the/etc/anacrontab, such as the first one, if the current time is more than one day later than the recorded event in the file. Then the anacrond process will execute the subsequent scheduled task and update the timestamp file in 65 minutes.


 

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.