Linux process Management-regularly perform tasks on a regular schedule

Source: Internet
Author: User

0. command to schedule a task:

At scheduling a job to execute at a moment

Batch schedules jobs to execute when system load is not heavy

Crontab scheduling jobs that run periodically

  

1.at command usage:

To schedule a command or multiple commands to run at a specified time

Syntax

at [-F filename] Time

at-d or ATRM Delete a task in a queue

At-l or ATQ view the tasks in the queue

1.1 at the specified time( as long as the ATD service is turned on, you need to check to see if the system turns on ATD service )

Absolute timing method;

Midnight Noon Teatime

Hh:mm[today]

HH:MM tomorrow

HH:MM Week

hh:mm Mm/dd/yy

Relative Timing Method:

Now +n minutes

Now +n hours

Now +n days

For example:

1.2 At Usage:

1. Scheduling Scheduled Tasks

For example: Send a broadcast in 10 minutes:

[Email protected] ~]# at now +  minutes  #用相对时间at >/usr/bin/wall </etc/motd< Eot> #输入命令之后Ctrl +d Save Task

Attention:

  (1) command to use absolute path.

(2) The prerequisite for using at timed tasks is to turn on the ATD service, so you need to check if the system is on ATD service:

[Email protected] ~]# ps-le| grep ATD 4 S     0   529     1  0     0 -  6460 hrtime?        xx:xx ATD

2. View and Delete Scheduled tasks

    • Timing Task Principle:

We have scheduled a scheduled task to actually save our commands under the/var/spool/at/directory:

[[email protected] ~]# ls/var/spool/at/ #查看目录下文件 a0000301832fd6  ~]# Tail-5 /< C5>var/spool/at/a0000301832fd6 #查看文件内容          1}${shell:' marcindelimiter2474a99d '/usr/bin/wall  </etc/motdmarcindelimiter2474a99d

We found that Linux actually put our scheduled tasks at the end of the file, and in the front we did a series of read environment variables, that is, our scheduled task is written at the end of the file.

    • To view and delete scheduled tasks:
[[Email protected] ~]# Atq[[email protected]~]# at now +5minutes #新增一个任务 at>/usr/bin/wall gggg<eot>Job4At Sat Mar to  the: at:xx 2018[[Email protected]~]# at now +Tenminutes #新增一个任务 at>/usr/bin/wall secondwall<eot>Job5At Sat Mar to  the: in:xx 2018[[Email protected]~]# atq #查看定时任务5Sat Mar to  the: in:xx 2018a root4Sat Mar to  the: at:xx 2018a root[[email protected]~]# AT-l #查看任务方法二5Sat Mar to  the: in:xx 2018a root4Sat Mar to  the: at:xx 2018a root[[Email protected]~]# at-d5 #移除序号是5的任务[[Email protected]~]# ATQ4Sat Mar to  the: at:xx 2018a root[[email protected]~]# ATRM4 #移除序号是4的任务

The 3.at configuration file restricts which users can use the AT command:

/etc/at.allow

/etc/at.deny

Rules:

If the/etc/at.allow file exists, only users who are listed in this file can use the AT command;

If the/etc/at.deny file exists, only users who are listed in this file will not be able to use the AT command;

If none of the two files exist, then only the superuser can use the AT command;

If all two files are present and empty, then everyone can use the AT command.

2.batch Command usage:

Usage and functionality are just like at, except that it checks the system load values and the load is lighter to perform tasks (typically less than 0.8 is lighter). The load value is the load averagewhen we query with W or top.

3.crontab the use of periodic execution commands: (dependent on the Crond process)

Role: The crontab file required to generate the cron process

Grammar:

crontab {-L |-e|-r}

-L Displays the current crontab

-R Delete Current crontab

-e Edit the current crontab file using the editor

CRONTAB-E format: (time value of filling in the method: will know the time value of filling, do not know to write *. )

Minutes Hours Day Month Week command/script

For example:

(1) Daily four o'clock in the morning backup files:

0 4 * * * Backup script

(2) Every Tuesday and Friday six o'clock in the afternoon backup files

0 18 * * 2,5 backup script

(3) Week 2 and Friday of January to March backup files

0 18 * 1-3 2,5 backup script

(4) Another demand, from Monday to Friday 17.30 issued a broadcast, 17.45 all computers automatically shut down

* * 1-5/usr/bin/wall < News.txt

* * 1-5/sbin/shutdown-h now

(5) March-June and September-December each week of the Week 1 to week 5 of 12 to 2 o'clock in the afternoon every 2 minutes to execute a backup script

*/2 12-14 * 3-6,9-12 1-5/bin/cp/root/data/-r/root/data.bak 2>/root/bak.err

The error message is redirected to the/root/bak.err folder.

Summary: Time range with-, multiple time with, comma-separated, not two minutes can be */2 in minutes, every two hours in the location of the day */2.

Summarize:

(1) These options cannot be empty and must be filled in

(2) If you do not know the specific value with * instead

(3) Each time period can be specified multiple values, separated by commas, the time range of the words-

(4) The command should give the absolute path

(5) The user must have permission to run the corresponding command or program.

1. Examples of recurring tasks:

(1) Send a broadcast every 3 minutes, content read from News.txt

[Email protected] ~]# CRONTAB-E

Enter the following, and then save the input when you are done:

*/36 /usr/bin/wall </root/news.txt

After saving can be viewed in the/var/spool/cron/directory

[[email protected] cron]# pwd/var/spool/cron[[email protected] cron]# cat root*/3 6 /usr/bin/wall </root/news.txt

The principle of timed execution:

We can also edit this file manually by saving the commands we wrote to the/var/spool/cron/directory, naming them, and then executing the files on a regular basis.

(2) Other examples:

2. Two ways to view recurring tasks:
[[email protected] cron]# CAT/var/spool/cron/root #直接读取文件 */36 /usr/bin /wall </root/news.txt* *1 * * * */usr/bin/-l #用crontab命令读取 */3
    6 /usr/bin/wall </root/news.txt*/1 * * * */usr/bin/Ls[[email protected] cron]#

3. Two ways to delete recurring tasks

The first type: delete all tasks directly

[Email protected] cron]# crontab-- for Root

The second type: Edit the/var/spool/cron/root file, delete the specified line and complete the delete task

Note: The Crond cycle execution task relies on the crond process, so the crond process needs to be turned on:

[[Email protected] ~]# pkill crond #杀掉进程[[Email protected]~]# Ps-le |grep Crond[[Email protected]~]# which Crond/usr/sbin/Crond[[email protected]~]# crond #开启crond进程[[email protected]~]# Ps-le |grep Crond1S0 22973     1  0   the   0-31554Hrtime?xx:xx:xxCrond

4.crontab configuration file:/etc/crontab file describes the format of the configuration file
[Email protected]/]# cat/etc/crontab #查看/etc/crontab file SHELL=/bin/Bashpath=/sbin:/bin:/usr/sbin:/usr/Binmailto=root# for details see Mans4crontabs# Example of Job definition:#.----------------Minute (0- -)# | .-------------Hour (0- at)# |  | .----------Day of Month (1- to)# |  |  | .-------Month (1- A) OR jan,feb,mar,apr ... #|  |  |  | .----Day of Week (0-6) (sunday=0Or7) OR sun,mon,tue,wed,thu,fri,sat#|  |  |  | |# * * * * * * user-name Command to be executed0*/6* * * * root/bin/wipefs

5./etc/There are weekly, daily and daily script folders executed, and we put a script in it to implement the corresponding execution script:

[Email protected] etc]# ll/etc/|grep cron-RW-------.1Root root541Mar to   .ANACRONTABDRWXR-xr-x.2Root root4096The -   .CRON.DDRWXR-xr-x.2Root root4096The -   .cron.daily-RW-------.1Root root0Mar to   .CRON.DENYDRWXR-xr-x.2Root root4096JuneTen   theCRON.HOURLYDRWXR-xr-x.2Root root4096JuneTen   thecron.monthly-rw-r--r--.1Root root480Jan +  ,: -CRONTABDRWXR-xr-x.2Root root4096JuneTen   theCron.weekly

CENTOS7, the call executes the script below the daily, monthly, and weekly folders below:/etc/anacrontab through Anacron in the call

[Email protected] etc]# cat/etc/anacrontab#/etc/anacrontab:configuration file foranacron# See Anacron (8) and Anacrontab (5) fordetails. SHELL=/bin/Shpath=/sbin:/bin:/usr/sbin:/usr/Binmailto=root# The maximal random delay added to theBasedelay of the Jobsrandom_delay= $# The jobs would be started during the following hours Onlystart_hours_range=3- A#periodinchDays delayinchMinutes job-identifier Command1       5cron.daily Nice run-parts/etc/cron.daily7        -Cron.weekly Nice run-parts/etc/cron.weekly@monthly $cron.monthly Nice run-parts/etc/cron.monthly

6. Limit which users can use the Crond command:

/etc/cron.allow

/etc/cron.deny

Rules:

If the/etc/cron.allow file exists, only users who are listed in this file can use the AT command;

If the/etc/cron.deny file exists, only users who are listed in this file will not be able to use the AT command;

If none of the two files exist, then only the superuser can use the AT command;

If all two files are present and empty, then everyone can use the AT command.

Linux process Management-regularly perform tasks on a regular schedule

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.