Linux Scheduled Tasks

Source: Internet
Author: User

First, what is called the planning task

The task of planning, literally understanding, is to perform the planned work at the agreed time. In Linux we often use the Cron service to do the job. For example, we can use Cron to make a backup log file 12 o'clock every night, which is a scheduled task.

Ii. Benefits of planning tasks

Like some do operation and maintenance work, usually there will be a lot of repetitive work, such as fixed-point backup, regular restart services, on-line services, regular testing and so on, and some of these tasks need to be carried out in the middle of the night, if we wait until midnight to operate will be very tired, very cumbersome, so we have to use the task of , we can drink tea, do some other things, also do not need to stay up late to work overtime.

Iii. Types of Scheduled Tasks

1. Work performed by the system: the work to be performed by the system periodically, such as backing up system data, cleaning up the cache
2. Work performed by an individual: a user's regular work, such as checking the mail server for new letters every 10 minutes, which can be set by each user.

Iv. implementation of the planned tasks

The command--at,batch, which is used to perform scheduled tasks, crontabs the command associated with a recurring task.
Differences between the 1.at command and the batch command
The AT command is intended to perform a task between specific tasks; The tasks set by the batch command are selected by the system for idle time, while idle time defaults to a CPU utilization rate of less than 0.8
2.at command
Whether the at task can run depends on the ATD service
We can check if the ATD service is running
Centos6:service ATD Status
CENTOS7:SYSTEMCTL Status ATD

Grammar
at (option) (parameter)
Options
-F: Specifies the task file containing the specific instructions;
-Q: Specifies the queue name for the new task;
-L: Displays a list of tasks to be performed;
-D: Deletes the specified pending task;
-M: Sends an e-mail message to the user after the task executes.
Instance


The exact time after the AT command wants to execute the program, and then enter
Then after > enter the command you want to execute, and finally use the CTRL+D key combination to exit at, in the image it will execute the LS at 16:54, and then send the results to root, we can see in the/var/spool/mail/root
We'll catch up with tomorrow in time and let it run sometime tomorrow, or follow the exact date 17:04 04/04/2018 that's what it's going to do on April 3, 2018 17:04
Or at now +10 minutes 10 minutes after execution
Of course, more than minutes this keyword can be used, the following keywords can be used
Minutes: "Minutes".

hours:表示《小时》。days:表示《天》。weeks:表示《星期》。months:表示《月》。years:表示《年》。

ATQ and ATRM commands: List and delete at tasks awaiting execution
Each time we use the AT command to specify the delay execution of the command, at will assign it a job number, such as the above example, we at the task number is 4, below I will show you how to operate the specific

After the task is deleted, it will not be executed again.
We can also use at-l to see the tasks that we want to delay execution.
At also has blacklist and whitelist function

(blacklist): When only/etc/at.deny exists (default) the user writing to deny cannot configure the at
(whitelist):/etc/at.deny fails when/etc/at.allow exists, only users written in allow can configure the at
When/etc/at.allow and/etc/at.deny are not present, only root can be configured at
The usage of the bath command is similar to at, and is no longer described here.

2.crontab command

Before we introduced the AT command, here I would like to introduce a more powerful command, that is, crontab, we use the AT command to execute only one command at a time, but crontab can execute the command repeatedly, such as: every minute, hourly, daily, weekly, etc.

Crontab is actually a command to read and modify files named Crontab. This crontab file contains a list of programs you want to execute on a timed basis, and also contains the time of execution.

In fact, there are two commands, one called Crontab, the other called Cron. Crontab is used to modify the crontab file, and Cron is used to actually execute timed programs.
There are three common parameters:

-e:修改crontab文件。-l:显示crontab文件。-r:删除crontab文件

/etc/crontab master configuration file for system-level scheduled Tasks
It can only be defined by root, but it can be used as a normal user to execute commands in a scheduled task.
The contents are as follows:

SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/# For details see man 4 crontabs# Example of job definition:# .---------------- minute (0 - 59)# |  .------------- hour (0 - 23)# |  |  .---------- day of month (1 - 31)# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat# |  |  |  |  |# *  *  *  *  * user-name command to be executed

The first four rows are the environment variables that are used to configure the Crond task to run, the shell variable specifies which shell the system will use, this is bash, and the second line of the path variable specifies the path to the System execution command. The third line of the mailto variable specifies that Crond's task execution information will be emailed to the root user, and if the value of the mailto variable is null, the task execution information is not sent to the user, and the home variable in line fourth specifies the home directory to use when executing the command or script.
The following comments are the configuration file format, and the corresponding meaning of each ※.
Here I will give you a detailed description of each paragraph of the definition:
The first paragraph should be defined as: minutes, which represents the number of minutes per hour to execute. Range is from 0-59
The second paragraph should be defined as: hours, which are executed from the first hours, ranging from 0-23
The third paragraph should be defined as: date, which is performed from the day ordinal of each month, ranging from 1-31
The fourth paragraph should be defined as: month, which represents the first months of the year to execute, ranging from 1-12
The fifth paragraph should be defined as: Week, which represents the day of the week of execution, ranging from 0-6, of which 0 represents Sunday.
Every six paragraphs should be defined: the user name, that is, the execution of the program to be executed by which user, this generally can be omitted;
The seventh paragraph should define the following: The command and parameters to execute.

We add content to the last line of the file such as:
5 * * * * ls
is to specify 5 minutes per hour to execute the LS command.
We can also add users before the command:
0 */2 * * * user1 ls

Specifies that the LS is executed every two hours as a User1 identity
We can also write some script files to be executed every day in one directory:
30 5 * * * root run-parts /etc/cron.daily
Specify all executables in the 5:30 execution/etc/cron.daily directory per day (if you want to execute a script individually, you need to put run-parts, followed by the script name)
We need to remember the meanings of several special symbols:

"*" represents a number in the range of values,
"/" stands for "every",

"-" represents a number to a number,

"," separate a few discrete numbers
Then every 2 hours, starting 10:30 every Monday to Friday, is
30 10-23/2 * * 1-5
PS: Every time we modify the crontab, we need to restart the service to take effect. The scripts we make are also given execute permissions, otherwise they cannot be executed.
The restart command is as follows:
/etc/rc.d/init.d/crond Restart #重启

/etc/rc.d/init.d/crond Reload #不中断服务, re-loading configuration
We also have a way to increase scheduling tasks:
Crontab-e then write the corresponding scheduled task, after writing to save the exit can be, this method writes the scheduling task is to generate a file with the same name as this user in/var/spool/cron, this user's cron information is recorded in this file, this file can not be directly edited, can only be edited with CRONTAB-E.
Ps:cron read this file once every time after startup, and check if you want to execute the command inside. Therefore, the Cron service does not need to be restarted after this file has been modified.
Crontab-r Delete all plans, not with the parameter is to delete the current user's schedule, and the user name is to delete a user's plan.

Linux Scheduled Tasks

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.