linux-Routine Schedule of work

Source: Internet
Author: User

1. Types of routine work scheduling

    • One is routine, that is, every certain period to be executed once;
    • One is sudden, that is, just do it once.

In order to achieve the above two functions, it is necessary to use the two commands of at and Crontab.

    • At:at is a command to finish scheduling only once, but to run at, you must have ATD support for this service, ATD may not start by default, then the AT command will fail.
    • Crontab:crontab the tasks configured by this command will be cycled through, with a cycle time of minutes, hours, weeks, months, or yearly. Crontab can also be used to edit/etc/crontab to support in addition to running commands. Similarly, if you want to make crontab effective, you must have crond this service support.

2. Run a scheduled work schedule

2.1 ATD Service

Before you start using the AT command, you need to check to see if the ATD service is running:

[[Email protected] ~] # service ATD Status

2.2 AT command

Work scheduling, in fact, is to generate work in some way, and put these tasks in the schedule, we use at this command to generate the work to run, and the work as a text file to the/var/spool/at/directory, the work will be able to wait for ATD This service access and operation.

In addition, we also have restrictions on the use of the account at this command, mainly through the/etc/at.allow and/etc/at.deny two files for the use of at the limit, plus the two files, at the work of the situation is this:

    1. First find/etc/at.allow This file, write in this file user can use at, no user in this file can not use at (even if not written in At.deny);
    2. If/etc/at.allow does not exist, find/etc/at.deny This file, if written in this At.deny user can not use at, and not in this At.deny file users, you may use at;
    3. If none of the two files exist, then only root can use the AT command.
[[Email protected] ~]# at [-MLDV] time[[email protected] ~]# at-c task work numberoptions and Parameters:-m: When the at job is completed, even if there is no output information, the user is notified by email that the work has been completed. -l:at-l List all the user's at schedules on the current system as ATQ .-d:at-d is equivalent to ATRM, can cancel a work in the at scheduling;-V: You can use the more obvious time format to list the work list in the at schedule;-C: You can list the actual command content of the work that is followed. Time: Date format, where you can define the "when to do at this work" time, in the format: hh:mm ex> 04:00at today's hh:mm time, if that moment is over, tomorrow's hh:mm will do the work. hh:mm YYYY -MM-DDEx> 04:00 2009-03-17It is mandatory to make this work at a particular time of day of the month of the year! hh:mm[am |PM] [Month] [Date]Ex> 04pm March 17also, force a certain time in a certain day of the year! hh:mm[am |PM] + number[minutes|hours| Days| weeks] Ex> now + 5 minutes ex> 04pm + 3Days means that at some point in time, it takes a few more minutes to proceed. 
Example: After five minutes, output inputto file/tmp/~] # at now+ 5 minutes"Hello" >/tmp/  Test.txtat> <EOT>   [Ctrl] + D will appear <EOF>4 at 2016-03-14 15:38#  This line of information indicates that the 4th at work will take place in 2016/03/14 15:38! 

When we use the at to enter an at Shell environment to let the user release the work command, it is best to use absolute path to release your command, because the command release is related to the path variable, and also related to the working directory at that time, so use absolute path to release the command, Would be a once in a way.

Because the at operation is independent of the terminal environment , all standard Output/standard error output is routed to the mailbox of the runner, and the terminal is not seeing any information. If you need to output information to the current terminal, you need to specify, for example, if you are currently logged in Tty1, use echo "Hello" >/dev/tty1.

  At specifies that the work is run in the background, when using at, the system will separate the item at work out of the current bash environment, directly to the system's ATD program to take over, so when the work of the release of the at the immediately offline, the rest of the work is completely to the Linux management Can.

Management of 2.3 at work

[[Email protected] ~] #  atq<== See how many at work schedules are currently on the host  [Email protected] ~]#  atrm jobnumber<== to remove the corresponding work 

2.4 Batch

Batch release of the work schedule will be available in the system when the background task, in fact, the use of at to command the release, just added some control parameters. This command will perform the assigned task only when the CPU workload is less than 0.8.

3. Routine work scheduling for cyclic operation

3.1 Cron (Crond) system services

The routine work schedule of the cyclic operation is controlled by the cron (Crond) system service, which needs to be checked to see if the service is started when using the instruction crontab.

3.2 How the crontab instruction works

The crontab command is also limited by the use of the file/etc/cron.allow and/etc/cron.deny two files, and the specific rules are limited by the AT command.

After you create a schedule with crontab, the work is recorded in the/var/spool/cron/and is identified by the account number. For example, WY uses crontab, his work will be recorded in the/var/spool/cron/wy, but it should be noted that do not use VI directly edit the file, because the input syntax may be incorrect, will cause the cron can not be run. 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.

 [[email protected] ~]# 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 schedule; -e: Edit Crontab's work -l: Review Crontab's work -r: Remove all crontab work, and if you want to remove only one item, use -E to edit. 
Example: With WY's identity in the daily~]$ crontab-e#  This time will go into the edit screen of VI let you edit the work, each job is a row. 0  * "at"   . BASHRC# Time- Sharing week |<============== command string ========================>|

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

for meaning

minutes hours date month week command
Number Range 0-59 0-23 1-31 1-12 0-7 (0 or 7 for Sunday) Command

The meaning of some auxiliary special characters:

Special characters Representative meaning
* Asterisk Representing any moment of acceptance
, Comma Represents the separation period
- Minus sign Represents a period of time in the range
/ N Slash That n represents the number, which is the meaning of every n unit interval

3.3 Configuration files for the system:/etc/crontab

Cron The minimum detection limit for this 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 the/etc/crontab file and store it, then The cron configuration is automatically run.

Here's the run-parts command, which is the function of taking all the files in the directory behind it to run.

3.4 Precautions

    • The problem of uneven allocation of resources
    • Cancel an output item that you don't want
    • Safety inspection
    • The week and the sun cannot coexist simultaneously

4. Can wake up work tasks during downtime

If our Linux host is not used as a 24-hour, all-year-round server, there may be some work schedules that cannot be performed on our machines on time, and how do the tasks in these downtime work? At this point we need to anacron this command to help us run the crontab task when we boot.

Anacron exists to handle the crontab of Linux systems that are not 24 hours long.
Anacron can not specify when to run a task, but in days or immediately after the start of the Anacron action, he will be to detect the outage period should be carried out but did not perform the crontab task, and run the task again, Anacron will automatically stop.

Anacron will be on a day, seven days, one months to detect the system does not carry out crontab tasks.

Anacron works: Anacron will analyze the current time and time recorded in the last run Anacron time, the comparison, if found to be different, that is, at some point did not carry out crontab task. At this point, Anacron will start to run the crontab task. So Anacron actually runs through the crontab. As a result, the Anacron run typically has two times, one is run during system startup, and one is written to the crontab schedule. This makes it possible to analyze the crontab tasks that the system does not perform at a specific time.

4.1 Anacron and/etc/anacrontab

Anacron is actually a program rather than a service, and this program has entered the crontab schedule in CentOS.

Syntax for Anacron:

[[Email protected] ~] # Anacron [-SFN] [job]: [Email protected] ~]# anacron-u [job] : options and Parameters:-s  : Start a continuous operation of each job (job), according to the data of the time record to determine whether to proceed;-F  : Forced, not to judge time stamp of the record; n  : Do not perform the task immediately, without delaying (delay) waiting time;-u  /etc/anacrontab the name of each job defined.

linux-Routine Schedule of work

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.