文章目录
十四、系统的延迟任务与定时任务
14.1 系统延迟任务
14.1.1延迟任务的发起
[root@rhel8 Desktop]$ at 02:02 #发起时间
> rm -fr /mnt/file1 #发起内容
>#按键crtl+d
[root@rhel8 Desktop]$ at -l #查看任务队列
[root@rhel8 Desktop]$ at -c 2 # 查看任务内容2
[root@rhel8 Desktop]$ at -r 2 # 取消任务2
14.1.2 当延迟任务有输出
- 输出会以邮件的形式发送到任务发起者的邮箱中
(1)首先你需要安装mailx postfix
[root@rhel8 Desktop]$ yum install mailx postfix
(2)开启postfix服务
[root@rhel8 Desktop]$ systemctl start postfix
[root@rhel8 Desktop]$ systemctl enable postfix
(3) 邮箱命令使用使用
- 查看邮箱
[root@rhel8 Desktop]$ mail -u root #查看邮箱
q #退出
& 1 #输入邮件编号查看
- 清空邮箱
[root@rhel8 Desktop]$ > /var/mail/root
(4)当延迟任务有输出时他会把输出用邮件方式发到本机邮箱
14.1.3 延迟任务权限
[root@rhel8 Desktop]$ vim /etc/at.deny # 延迟任务的控制黑名单。此名单的人不能进行延迟任务
[root@rhel8 Desktop]$ vim /etc/at.allow # 一般来说此文件不存在,当此文件存在时at.deny就不生效,此文件中的人可以执行延迟任务
14.2 定时任务【crond】
14.2.1 定时任务发起
- 任务存储位置:/var/spool/cron/root
- 系统级别cron设定文件存放路径:/etc/cron.d
- cron设定文件 vim /etc/cron.d/text
- 命令设定方式:
[root@rhel8 Desktop]$ crontab -u root -e #设定定时任务
[root@rhel8 Desktop]$ crontab -u root -l #查看定时任务
[root@rhel8 Desktop]$ crontab -u root -r #删除定时任务
- 设定形式:
- 例子
时间 用户 动作
* * * * * root rm -fr file
14.2.2 定时任务权限
(1)定时时间权限文件
[root@rhel8 Desktop]$ vim /etc/cron.daily/ #每天任务
[root@rhel8 Desktop]$ vim /etc/cron.hourly/ #每小时任务
[root@rhel8 Desktop]$ vim /etc/cron.monthly/ #每月任务
[root@rhel8 Desktop]$ vim /etc/cron.weekly/ #每周执行动作
(2)定时用户权限文件
[root@rhel8 Desktop]$ vim /etc/cron.deny #cron用户黑名单,用法同at.deny
[root@rhel8 Desktop]$ vim /etc/cron.allow #cron白名单,用法同at.allow