Plan task

Time:2022-5-7

Through task planning, the system can automatically execute tasks according to time or periodic tasks

 

Environmental preparation

[[email protected] ~]#yum -y install postfix
[[email protected] ~]#systemctl enable --now postfix

 

Perform a task at a certain point in the future

  • At specifies a point in time to perform a one-time task
  • The batch system chooses its own free time to perform the tasks specified here

Run a task periodically

  • cron

One time task

At tool

  • Provided by package at
  • It depends on ATD service and needs to be started to realize at task
  • The at queue is stored in the / var / spool / at directory, and Ubuntu is stored in the / var / spool / cron / atjobs directory

  • When executing a task, the value of the path variable is consistent with the user identity of the currently defined task

At command:

at [option] TIME

Common options:

-V display version information
-T time time format [[CC] YY] mmddhhmm [. SS]
-L list the jobs waiting to run in the specified queue; Equivalent to atq
-D. delete the designated n-number operation; Equivalent to ATRM
-C n view specific job n task
-Read task in the file specified by F file
-M when the task is completed, an email will be sent to the user, even if there is no standard output

At time format:

HH: mm is performed at HH: mm today. If the time has passed, perform the task at this time tomorrow
02:00
HH: mm yyyy-mm-dd stipulates that the task shall be carried out at a special time on a certain day of a certain month of a certain year
02:00 2016-09-20
HH:MM[am|pm] [Month] [Date]
06pm March 17
17:20 tomorrow
HH: mm [am | PM] + number [minutes | hours | days | weeks], add a few more times at a certain point in time
Task
now + 5 min
02pm + 3 days

example:

echo touch /root/at.log | at 9:34

At task execution method:

  • interactive
  • input redirection
  • at -f file

/etc/at. {allow, deny} controls whether users can perform at tasks

  • Whitelist: / etc / at Allow does not exist by default. Only users in this file can execute the at command
  • Blacklist: / etc / at Deny exists by default and refuses the user in the file to execute the at command instead of at The user in the deny file is executable
  • If neither file exists, only root can execute the at command

Periodic task planning Cron

Periodic task plan cron related packages:

  • Cronie: the main package, which provides crond daemon and related auxiliary tools
  • Crontabs: contains system maintenance tasks provided by CentOS
  • Cronie anacron: the supplementary program of cronie, which is used to monitor the execution status of cronie tasks. For example, if the task in cronie fails to run normally at the time point in the past, anacron will start this task later

Cron relies on the crond service to ensure that the crond daemon is running:

#CentOS 7 and later:
systemctl status crond
#CentOS 6:
service crond status

Cron tasks are divided into

  • System cron tasks: system maintenance job, / etc / crontab main configuration file, / etc / cron D / sub profile
  • User cron tasks: saved in / var / spool / cron / username (Ubuntu system is stored in / var / spool / cron / crontabs / username), and managed by crontab command

Scheduled task log: / var / log / Cron

User scheduled tasks

  • Each user has a dedicated cron task file: / var / spool / cron / username
  • The default standard output and errors will be emailed to the corresponding users. For example, the tasks created by Wang will be sent to Wang’s mailbox
  • Root can modify jobs of other users
  • The default path in the user’s cron is / usr / bin: / bin. If other paths are used, add path = / path to the first line of the task file or add it to the script for scheduled task execution
  • The sixth field specifies the command to run. The entire command part of the line, up to the newline character or the ‘%’ character, is executed by the specified shell Unless escaped with a backslash (\), the ‘%’ character in the command will become a newline character and all data after the first% will be sent to the command as standard input.

Crontab command format:

crontab [-u user] [-l | -r | -e] [-i]

Common options:

-L list all tasks
-E edit task
-R remove all tasks
-I is used with - r to remove the specified task in interactive mode
-U user specifies the user to manage cron tasks, which can only be run by root

Control users to perform scheduled tasks:
/etc/cron.{allow,deny}

Add default editor (VI by default)

#CentOS[[email protected] ~]# vim /etc/profile.d/env.sh 

PS1='\[\e[1;34m\][\[email protected]\h \W]$ \[\e[0m\]'
export EDITOR=vim #ubunt

[email protected]:~# vim .profile

# ~/.profile: executed by Bourne-compatible login shells.

if [ “$BASH” ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi

mesg n || true
export EDITOR=vim

 

 

Common options for customizing the plan: task: execute disk scanning script every ten minutes

crontab -e

 

Specify the path variable in the script
[[email protected] ~]#crontab -l
* * * * * /data/test.sh
[[email protected] ~]#cat /data/test.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
useradd hehe
echo $PATH
[[email protected] ~]#cat /usr/bin/disk_check.sh
#!/bin/bash
#
#********************************************************************
#Author: wangxiaochun
#QQ: 29308620
#Date: 2020-05-08
#FileName: disk_check.sh
#URL: http://www.wangxiaochun.com
#Description: The test script
#Copyright (C): 2020 All rights reserved
#********************************************************************
WARNING=10
df | sed -En '/^\/dev\/sd/[email protected]^([^ ]+).* ([0-9]+)%.*@ @p'| while read DEVICE
USE;do
[ $USE -gt $WARNING ] && echo "$DEVICE will be full,USE:$USE" | mail -s
diskfull root
done
[[email protected] ~]#crontab -l
*/10 * * * * check_disk.sh

Example 2

[[email protected] ~]#cat check_disk.sh
#!/bin/bash

df | awk -F ' +|%' '/^\/dev\/sd/{if($5 > 10){system("echo "$1" will be
full,use:" $5 "| mail -s warning [email protected]")} }'
[[email protected] ~]#crontab -l
*/10 * * * * /root/check_disk.sh

Example 3

[[email protected] ~]#cat check_disk2.sh
#!/bin/bash
WARNING=2
df | awk -F ' +|%' '/^\/dev\/sd/{print $1,$5}'|while read DISK USE;do
if [ $USE -gt $WARNING ];then
echo "$DISK will be full,use:$USE" | mail -s diskwarning
[email protected]
fi
done
[[email protected] ~]#crontab -l
*/10 * * * * /root/check_disk2.sh

 

example:

[[email protected] ~]#man 5 crontab
For example, "0-23/2" can be used in the 'hours' field to specify command
execution for every other hour (the alternative in the V7 standard is
"0,2,4,6,8,10,12,14,16,18,20,22")

#For example, "0-23 / 2" can be used in
#The 'hours' field specifies that the command is executed every other hour (0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22 in V7 standard).

 

 

Example: the relationship between the date of each month and the day of the week field

[[email protected] ~]#man 5 crontab
Note: The day of a command's execution can be specified in the following two
fields — 'day of month', and 'day of week'. If both fields are restricted
(i.e., do not contain the "*" character), the command will be run when either
field matches the current time. For example, "30 4 1,15 * 5" would cause a
command to be run at 4:30 am on the 1st and 15th of each month, plus every
Friday.

Note: the execution date of the command can be specified in the following two fields – ‘day of’Month and day of week.If both fields are restricted (i.e. do not contain the “*” character) –Character), the command runs when any field matches the current time.For example,“30 4 1,15 * 5” will cause the command to run at 4:30 a.m. on the 1st and 15th of each month,Plus every Friday.

There are too few default path variables for scheduled tasks. Add the path variable at the beginning

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

 

 Interview question: perform / APP / bin / test every 2 hours between 6-12 o’clock every day in November sh

[[email protected] ~]#crontab -l
0 6-12/2 * 11 * /app/bin/test.sh

% is not recommended in Cron task. It has special purpose. It represents the special meaning of line feed, and all strings after the first% will be deleted
As the standard input of the command, if you want to use% in the command, you need to use \ escape
Note: placing% in single quotes is not supported

Example: usage of% in crontab

30 2 * * * /bin/cp -a /etc/ /data/etc`date +\%F_\%T`
30 2 * * * /bin/cp -a /etc//data/etc`date +‘%F_% T '` problem

Example: usage of% in crontab

[[email protected] ~]#crontab -l
* * * * * mail -s "test" wang%wang,%%how are you?%
[[email protected] ~]# cat /var/spool/mail/wang
From [email protected] Sat Jul 4 23:58:01 2020
Return-Path: 
X-Original-To: wang
Delivered-To: [email protected]
Received: by centos8.localdomain (Postfix, from userid 0)
id 0B03860272; Sat, 4 Jul 2020 23:58:01 +0800 (CST)
Date: Sat, 04 Jul 2020 23:58:01 +0800
To: [email protected]
Subject: test
User-Agent: Heirloom mailx 12.5 7/5/10
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Message-Id: <[email protected]>
From: [email protected] (root)
wang,
how are you?
[[email protected] ~]#

 

Recommended Today

Python panel time series data prediction: Granger causality test, Granger causality test, drug sales example and visualization

Original link:http://tecdat.cn/?p=23940 Time series is based on fixed time_ Interval_ Recorded observation sequence. This guide takes you through the process of analyzing the characteristics of a given time series in Python. content What is time series? How do I import time series in Python? What is panel data? Visualization of time series Patterns in time […]