#####1.延迟任务###################
一.延迟任务的发起
[root@rhel8 ~]# watch -n1 ls -lR /mnt/ ##打开监控面板
[root@rhel7 ~]# date ##查看现在时间
[root@rhel8 ~]# at 17:59 ##设置在17:59分的时候执行命令
at> rm -fr /mnt/* ##删除/mnt下的所有内容
at> <EOT> ##【ctrl】+【d】任务发起
[root@rhel8 ~]# at -l ##查看任务队列
[root@rhel8 ~]# at -c 1 ## at -c 任务号 查看任务内容
[root@rhel8 ~]# touch /mnt/file{1..3} ##建立文件
[root@rhel8 ~]# at -r 2 ## at -r 任务号 取消任务
二.当延迟任务有输出,输出会以邮件形式发送到任务发起者邮箱中
[root@rhel8 ~]# dnf install mailx-12.5-29.el8.x86_64 ##安装查看邮件的工具
[root@rhel8 ~]# dnf install postfix -y ##安装邮件服务
[root@rhel8 ~]# systemctl start postfix.service ##打开postfix
[root@rhel8 ~]# systemctl enable postfix.service
[root@rhel8 ~]# mail root ##发邮件(.表示邮件发送)
[root@rhel8 ~]# mailq ##邮件发送
[root@rhel8 ~]# mail ##查看邮件
[root@rhel8 ~]# > /var/spool/mail/root ##清空邮件
[root@rhel8 ~]# mail -u root ##查看邮件
[root@rhel8 ~]# at 18:55 ##在 18:55执行timedatectl命令,此命令有输出但不会显示到字符设备中会以邮件的形式发送给at发起人
[root@rhel8 ~]# mail -u root ##查看
注意:
[root@rhel8 ~]# mail -u root
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/mail/root": 1 message
> 1 root Sun Feb 23 18:55 21/789 "Output from your job "
& 1 ##输入邮件编号查看
& q ##退出
三.at命令的控制
[root@rhel8 ~]# vim /etc/at.deny ##at控制黑名单,此名单中的人不能执行at
[root@rhel8 ~]# su - westos ##切换用户执行at命令(可执行)
[westos@rhel8 ~]$ at now+1min
[root@rhel8 ~]# su - lei ##切换用户执行at命令(不可执行)
[lei@rhel8 ~]$ at now+1min
[root@rhel8 ~]# vim /etc/at.allow ##①此文件默认不存在 ②此文件存在时/etc/at.deny不生效 ③当此文件存在系统普通用户 默认不能执行at,只有在名单中的人可以
[root@rhel8 ~]# touch /etc/at.allow ##建立文件
[root@rhel8 ~]# su - westos ##切换用户执行之前的at命令(不可执行)
[westos@rhel8 ~]$ at now+1min
[root@rhel8 ~]# vim /etc/at.allow ##编辑文件
[root@rhel8 ~]# su - westos ##依然无法执行
[westos@rhel8 ~]$ at now+1min
[root@rhel8 ~]# su - lei ##切换用户执行之前的at(可执行)
[lei@rhel8 ~]$ at now+1min
[root@rhel8 ~]# cat /etc/at.deny ##at.allow存在 at.deny不生效
#####2.定时任务#################
crond ##定时任务服务
[root@rhel8 ~]# systemctl status crond ##查看服务状态
一.crond ##设定方式
命令设定方式:用户级别定时任务
crontab -u root -e | 设定 |
crontab -u root -l | 查看 |
crontab -u root -r | 删除 |
/var/spool/cron/root | 任务存储位置 |
编写方式:
时间 任务
分钟 小时 天 月 周 系统命令|脚本
注意:分钟0-59,小时0-23,天1-31,月1-12 ,周0-7(0和7都表示sunnday)
[root@rhel8 ~]# crontab -u root -e ##编辑
08(分) 23(小时) 23 (天) 02(月) 7 (周) rm -fr /mnt/*(命令)
[root@rhel8 ~]# crontab -u root -l ##查看任务
[root@rhel8 ~]# crontab -u root -r ##删除任务
[root@rhel8 ~]# cat /var/spool/cron/root ##任务存储位置
表示时间段:
[root@rhel8 ~]# crontab -u root -e
08-17 * * * * ##每天每小时08分-17分
08-17 * * * 3,5 ##每周3和每周5每小时08分-17分(3-5表示周三至周五)
08-17/2 * * * 3,5 ##每周3和每周5每小时08分-17分时间段每隔两分钟
08-17/2 10 * * 3,5 ##每周3和每周5十点08分-17分时间段每隔两分钟
08-17/2 10 5 * 3,5 ##每周3和每周5及每月5号十点08分-17分时间段每隔两分钟
08-17/2 10 5 3 3,5 ##3月每周3和每周5及3月5号十点08分-17分时间段每隔两分钟
文本设定:系统级别cron,只有超级用户可操作
[root@rhel8 ~]# cd /etc/cron.d ##系统及被cron,只有超级用户可操作
[root@rhel8 cron.d]# vim /etc/cron.d/westos
时间 用户 动作
* * * * * root rm -fr /mnt/*
[root@rhel8 cron.d]# touch /mnt/file{1..5} ##建立文件
/etc/cron.daily/ | 每天任务 |
/etc/cron.hourly/ | 每小时任务 |
/etc/cron.monthly/ | 每月任务 |
/etc/cron.weekly/ | 每周任务 |
例如:
[root@rhel8 cron.d]# cd /etc/cron.hourly/ ##每小时
[root@rhel8 cron.hourly]# vim clean_mnt
[root@rhel8 cron.hourly]# chmod +x clean_mnt ##赋予执行权限
二.用户级别crond控制设定
[root@rhel8 ~]# vim /etc/cron.deny ##cron用户黑名单,用法同at.deny
[root@rhel8 ~]# su - lei ##切换用户不可执行crontab -e 命令
[lei@rhel8 ~]$ crontab -e
[root@rhel8 ~]# su - westos ##切换用户可执行crontab -e 命令
[westos@rhel8 ~]$ crontab -e
[root@rhel8 ~]# ls -l /etc/cron.allow ##查看此文件不存在
[root@rhel8 ~]# touch /etc/cron.allow ##建立文件
[root@rhel8 ~]# su - westos ##默认用户将不能执行crontab -e命令
[westos@rhel8 ~]$ crontab -e
[root@rhel8 ~]# vim /etc/cron.allow ##cron用户白名单,用法同at.allow
[root@rhel8 ~]# su - lei ##切换用户可执行crontab -e命令
[lei@rhel8 ~]$ crontab -e