计划任务

计划任务主要是做一些周期性任务,目前主要用途是定期备份数据。

一次调度执行at

  • 语法格式

    1. at <TIMESPEC>
    2. now +5min
    3. teatime tomorrow (teatime is 16:00)
    4. noon +4 days
    5. 5pm august 3 2021
    6. ctrl+D退出任务编辑

    at允许使用一套相当复杂的指定时间的方法。
    1)能够接受在当天的hh:mm(小时:分钟)式的时间指定。假如该时间已过去,那么就放在第二天执行。 例如:04:00
    2)能够使用midnight(深夜),noon(中午),teatime(饮茶时间,一般是下午4点)等比较模糊的词语来指定时间。
    3)能够采用12小时计时制,即在时间后面加上AM(上午)或PM(下午)来说明是上午还是下午。 例如:12pm
    4)能够指定命令执行的具体日期,指定格式为month day(月 日)或mm/dd/yy(月/日/年)或dd.mm.yy(日.月.年),指定的日期必须跟在指定时间的后面。 例如:04:00 2009-03-1
    5)能够使用相对计时法。指定格式为:now + count time-units ,now就是当前时间,time-units是时间单位,这里能够是minutes(分钟)、hours(小时)、days(天)、weeks(星期)。count是时间的数量,几天,几小时。 例如:now + 5 minutes 04pm + 3 days
    6)能够直接使用today(今天)、tomorrow(明天)来指定完成命令的时间。

  • 安装at服务

    1. [root@localhost ~]# yum install at -y
  • 例1 五分钟后创建xiaohong用户(按ctrl+D退出任务编辑)

    1. [root@localhost ~]# systemctl start atd
    2. [root@localhost ~]# systemctl enable atd
    3. [root@localhost ~]# at now +5min
    4. at> useradd xiaohong
    5. at> <EOT>
    6. job 4 at Thu Mar 11 10:58:00 2021
    7. [root@localhost ~]# atq
    8. 4 Thu Mar 11 10:58:00 2021 a root
    9. [root@localhost ~]# id xiaohong
    10. id: xiaohong: no such user
    11. uid=1003(xiaohong) gid=1003(xiaohong) 组=1003(xiaohong)
    12. [root@localhost ~]# atq
    13. [root@localhost ~]#
  • 例2 两分钟后执行脚本at.jobs,内容是在/root目录创建以日期为名的文件

    1. [root@localhost ~]# vim at.jobs
    2. touch /root/`date +%F`.txt
    3. [root@localhost ~]# at now +2min < at.jobs
    4. job 6 at Thu Mar 11 11:11:00 2021
    5. [root@localhost ~]# ls
    6. anaconda-ks.cfg at.jobs test1
    7. [root@localhost ~]# ls
    8. 2021-03-11.txt anaconda-ks.cfg at.jobs test1
  • 例3 在当天11点17分,创建用户xiaowang

    1. [root@localhost ~]# at 11:17
    2. at> useradd xiaowang
    3. at> <EOT>
    4. job 7 at Thu Mar 11 11:17:00 2021
    5. [root@localhost ~]# atq
    6. 7 Thu Mar 11 11:17:00 2021 a root
    7. [root@localhost ~]# id xiaowang
    8. id: xiaowang: no such user
    9. [root@localhost ~]# date
    10. 2021 03 11 星期四 11:16:22 CST
    11. [root@localhost ~]# date
    12. 2021 03 11 星期四 11:18:14 CST
    13. [root@localhost ~]# id xiaowang
    14. uid=1004(xiaowang) gid=1004(xiaowang) 组=1004(xiaowang)
    15. [root@localhost ~]# atq
    16. [root@localhost ~]#

    循环调度执行cron用户级

  • cron执行之前必须启动守护进程

    1. [root@localhost ~]# systemctl start crond
    2. [root@localhost ~]# systemctl enable crond
    3. [root@localhost ~]# ps aux |grep crond
    4. root 6381 0.0 0.0 126284 1688 ? Ss 10:11 0:00 /usr/sbin/crond -n
  • crond进程会每分钟处理一次计划任务

  • 存储位置

    1. [root@localhost ~]# ls /var/spool/cron
  • 管理命令

    1. [root@localhost ~]# crontab -l # 列出当前用户所有计划任务
    2. [root@localhost ~]# crontab -r # 删除当前用户计划任务
    3. [root@localhost ~]# crontab -e # 编辑当前用户计划任务
    4. 管理员可以使用 -u username,去管理其他用户的计划任务
    5. [root@localhost ~]# vi /etc/cron.deny # 这个文件中加入的用户名无法使用cron
  • cron语法格式 ```bash 分 时 日 月 星期 命令

  • 表示任何数字都符合 0 2 /run.sh # 每天的2点 0 2 14 /run.sh # 每月14号2点 0 2 14 2 /run.sh # 每年2月14号2点 0 2 5 /run.sh # 每个星期5的2点 0 2 6 5 /run.sh # 每年6月份的星期5的2点 0 2 2 5 /run.sh # 每月2号或者星期5的2点 星期和日同时存在,那么就是或的关系 0 2 2 6 5 /run.sh # 每年6月2号或者星期5的2点 /5 /run.sh # 每隔5分钟执行一次 0 2 1,4,6 /run.sh # 每月1号,4号,6号的2点 0 2 5-9 * /run.sh # 每月5-9号的2点
          • /run.sh # 每分钟 0 /run.sh # 每整点
    • 2 /run.sh # 每月2号的每分钟
      1. <a name="bQTmU"></a>
      2. ## 循环调度执行cron系统级
      3. 系统任务执行在/etc/crontab里
      4. ```bash
      5. [root@localhost ~]# vim /etc/crontab # 默认没有定义任何计划任务
      6. [root@localhost ~]# ls /etc/cron.d # 定义的计划任务每个小时会执行
      7. 0hourly sysstat
      8. [root@localhost ~]# cat /etc/cron.d/0hourly
      9. # Run the hourly jobs
      10. SHELL=/bin/bash
      11. PATH=/sbin:/bin:/usr/sbin:/usr/bin
      12. MAILTO=root
      13. 01 * * * * root run-parts /etc/cron.hourly # 每小时01分以root身份执
      14. 行/etc/cron.hourly/目录下的所有脚本
  • crond仅仅会执行每小时定义的脚本,在/etc/cron.hourly ```bash [root@localhost ~]# ls /etc/cron.hourly/ 0anacron [root@localhost ~]# cat /etc/cron.hourly/0anacron

    !/bin/sh

    Check whether 0anacron was run today already

    if test -r /var/spool/anacron/cron.daily; then day=cat /var/spool/anacron/cron.daily fi if [ date +%Y%m%d = “$day” ]; then exit 0; fi

Do not run jobs when on battery power

if test -x /usr/bin/on_ac_power; then /usr/bin/on_ac_power >/dev/null 2>&1 if test $? -eq 1; then exit 0 fi fi /usr/sbin/anacron -s # anacron是用来检查是否有错过的计划任务需要被执行

  1. ```bash
  2. [root@localhost ~]# cat /etc/anacrontab
  3. # /etc/anacrontab: configuration file for anacron
  4. # See anacron(8) and anacrontab(5) for details.
  5. SHELL=/bin/sh
  6. PATH=/sbin:/bin:/usr/sbin:/usr/bin
  7. MAILTO=root
  8. # the maximal random delay added to the base delay of the jobs
  9. RANDOM_DELAY=45
  10. # the jobs will be started during the following hours only
  11. START_HOURS_RANGE=3-22
  12. #period in days delay in minutes job-identifier command
  13. #每天开机 5 分钟后就检查 /etc/cron.daily 目录内的文件是否被执行,如果今天没有被执行,那就执行
  14. 1 5 cron.daily nice run-parts /etc/cron.daily
  15. #每隔 7 天开机后 25 分钟检查 /etc/cron.weekly 目录内的文件是否被执行,如果一周内没有被执行,就会执行
  16. 7 25 cron.weekly nice run-parts /etc/cron.weekly
  17. #每隔一个月开机后 45 分钟检查 /etc/cron.monthly 目录内的文件是否被执行,如果一个月内没有被执行,就执行
  18. @monthly 45 cron.monthly nice run-parts /etc/cron.monthly