Linux_day05_02_ scheduled task

Scheduled Tasks

Linux scheduled tasks
  • Perform a task at a certain point in time in the future: at, batch
  • Run a task periodically: crond
  1. at command

Need to install: yum -y install at

[root@localhost ~]# at --help
at: invalid option -- '-'
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
       at [-V] [-q x] [-f file] [-mMlbv] -t time
       at -c job ...
       atq [-V] [-q x]
       at [ -rd ] job ...
       atrm [-V] job ...
       batch
  • Instructions: at parameter time
  • parameter
    • -m: When the specified task is completed, mail will be sent to the user, even if there is no standard output
    • -I: alias of atq
    • -d: alias of atrm
    • -v: display the time when the task will be executed
    • -c: Print the content of the task to standard output
    • -V: Display version information
    • -q: Use the specified queue
    • -f: Read tasks from the specified file instead of reading from standard input
    • -t: Submit the task to be run in the form of a time parameter
  • time
    • HH::MM [YYYY-MM-DD]
    • moon, midnight, teatime
    • tomorrow
    • now + num {minutes, hours, days, weeks}
  • atq: View scheduled tasks
  • atrm 2: Remove task

Start atd.service in advance: systemctl start atd

  1. batch command

Let the system automatically select idle time to perform the tasks specified here

  • Usage note: batch parameter time
  • parameter
    • -f: Specify a task file containing specific instructions
    • -q: Specify the queue name of the new task
    • -m: Send email to the user after the task is executed
  • Example
batch 
at> echo 1234
at> <EOT>
job 5 at Sun Apr 28 08:49:00 2013
Linux system tasks

The Linux system is controlled by the crond service to schedule tasks. There are many very many scheduled tasks on Linux, so this service is started by default. In addition, since users can also set up scheduled tasks, the crontab command is provided for users to control scheduled tasks

cat /etc/crontab 

SHELL=/bin/bash	指定shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin	系统执行命令的路径
MAILTO=root		任务执行通过邮件发送给root用户

# 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
0-30 */2 3,6,9 1 root /usr/bin/echo hello
  分 时 日 月 周 用户名 任务命令

time

  • Specific value: a value within the valid range at a given point in time
  • *: All values ​​within the valid value range at a given point in time, meaning "every..."
  • Discrete value: Discrete value 1,3,6,9 through comma
  • Continuous value: through the minus sign-can be continuous value 0-30
  • Definition step: */5: means "every 5..."

crontab command

  • -l: List all tasks
  • -e: edit task
  • -r: remove all tasks
  • -i: interactive
  • -u: Specify the user

Practice questions

  1. Set a one-time scheduled task to shut down the system at 18:00 and view task information
at 18:00
at> poweroff
at> <EOT>

atq # 查看任务信息
  1. Pack and compress /etc/passwd /etc/shadow /etc/group /etc/gshadow to file.tar.gz every night at 24:00
crontab -e -uroot 0 0 * * * tar -czvf file.tar.gz /etc/passwd /etc/shadow /etc/group /etc/gshadow
  1. List disk usage every five minutes every Monday
crontab -e -uroot */5 * * * 1 df -Th
  1. Synchronize time with Internet time synchronization server pool.ntp.org at 8:30 every day
crontab -e -uroot 30 8 * * * ntpdate pool.ntp.org
  1. View root's scheduled tasks through the crontab command, and list the corresponding file contents under /var/spool/cron through file viewing tools
crontab -l -uroot

cat /var/spool/cron/root

Guess you like

Origin blog.csdn.net/qq_44924544/article/details/108903885