Linux-计划任务at和cron

一. 计划任务

一次性任务 at
循环任务 cron
计划任务的输出默认以邮件形式发给用户,除非重定向

二. 一次性调动at

2.1 at

两种方式使用at:

  1. 直接写命令
[root@localhost ~]# at now+1min
warning: commands will be executed using /bin/sh
at> mkdir /tmp/1min.txt
at> mkdir /tmp/2min.txt
at> <EOT>
job 1 at Mon Feb  8 04:11:00 2021

2.命令写文件中,然后执行文件

# at now+1min < 1.txt

2.2 atq

查询存在的at任务

[root@localhost ~]# atq
2	Mon Feb  8 04:14:00 2021 a root

三. 循环调动执行cron

crond进程每一分钟处理一次计划任务

3.1 用户级别的cron

3.1.1 用户级别的计划任务存放位置

可以直接编辑该目录下文件来编辑用户级计划任务
/var/spool/cron

3.1.2 管理方式

  1. 列出当前用户所有用户级计划任务
[root@DTOS cron]# crontab -l
no crontab for root
  1. 删除当前用户所有用户级计划任务
[root@DTOS cron]# crontab -r
  1. 编辑计划任务
    [root@DTOS cron]# crontab -e
  2. 管理员可以用-u来编辑其他用户的用户级计划任务
[root@localhost cron]# crontab -lu andy
[root@localhost cron]# crontab -eu andy
[root@localhost cron]# crontab -ru andy

  1. 禁止某些用户使用用户级cron
    将其添加进/etc/cron.deny文件
[root@localhost cron]# cat /etc/cron.deny 
andy

3.1.3 job格式

在这里插入图片描述

0 2 14 * 7 ls                                      //每月14日的两点或每周日的两点
0 2 3-5 * * ls                                      //每月3日到5日的两点
0 2 3,5 * * ls                                      //每月3日和5日的两点
*/5 * * * * ls                                      //每隔5分钟

3.2 系统级别的cron

3.2.1 配置位置一 /etc/crontab

vi /etc/crontab

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=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

3.2.2 配置位置二 /etc/cron.d/*

/etc/cron.d/下所有文件

  1. 把系统级别计划任务写在/etc/cron.d目录中的文件里
  2. 其中这个目录下的0hourly文件比较特殊,每小时执行一次目录/etc/cron.hourly下的所有脚本
    在这里插入图片描述
  3. 而目录/etc/cron.hourly下的文件0anacron很重要,他每小时一次的去启动anacrond这个程序
    在这里插入图片描述
[root@localhost cron.d]# cat /etc/cron.hourly/0anacron 
#!/bin/sh
# Check whether 0anacron was run today already
/usr/sbin/anacron -s
  1. 而anacrond进程的配置文件在/etc/anacrontab
    里面有三行数据分别是:
    4.1 每一天,延时5分钟执行一次/etc/cron.daily目录中所有脚本,是否执行需要对比/var/spool/anacron/cron.daily 这个文件中记录的时间,如果该文件记录的时间早于一天就执行
    4.2 每一周,延时25分钟执行一次/etc/cron.weekly目录中所有脚本,是否执行需要对比/var/spool/anacron/cron.weekly 这个文件中记录的时间
    4.3 每月,延时45分钟执行一次/etc/cron.monthly目录中所有脚本,是否执行需要对比/var/spool/anacron/cron.monthly 这个文件中记录的时间
[root@localhost license]# cat /etc/anacrontab 
# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22

#period in days   delay in minutes   job-identifier   command
1	5	cron.daily		nice run-parts /etc/cron.daily
7	25	cron.weekly		nice run-parts /etc/cron.weekly
@monthly 45	cron.monthly		nice run-parts /etc/cron.monthly

四. crond脑图

在这里插入图片描述

五. anacrond脑图

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值