#######系统延时任务及定时任务########
##1 系统延时任务
课堂实验:
接收端
[root@westosaaaaaa ~]# touch /mnt/westos{1..10}
[root@westosaaaaaa ~]# watch -n 1 ls -l /mnt/
发送端
[root@westosaaaaaa ~]# at 12:15
warning: commands will be executed using /bin/sh
at> rm -fr /mnt/*
at> <EOT> << CTRL + D ##CTRL + D发起任务
job 4 at Thu Nov 11 12:15:00 2021
[root@westosaaaaaa ~]# at now+1min ##延时1分钟
at> rm -fr /mnt/*
at> <EOT>
at -l ##查看任务列表
at -c 任务号 ##查看任务内容
at -r 任务号 ##取消任务执行
注意:
当任务有输出时,输出会以邮件的形式发送给 at 任务的发起者
mail -u root ##查看超级用户邮件
1 ##查看第一封邮件
q ##退出
##2 at任务用的黑白名单
/etc/at.deny ##系统中默认存在,在此文件中出现的用户不能执行at
/etc/at.allow ##系统中默认不存在,当文件出现,普通用户不能执行at
##只有在名单中的用户可以,并且/etc/at.deny失效
课堂实验:
[root@westosbbbbbbb ~]# vim /etc/at.deny ##设置黑名单用户lee
#lee
[root@westosbbbbbbb ~]# su - lee ##切换lee用户
[lee@westosbbbbbbb ~]$ at now+imin ##设定不可访问
You do not have permission to use at.
[root@westosbbbbbbb ~]# vim /etc/at.allow ##同理,设定白名单
115 vim /etc/chrony.conf
119 touch /mnt/westos{1..10}
122 watch -n 1 ls -l /mnt/
19 vim /etc/chrony.conf
20 systemctl restart chronyd.service
## 3 设定系统定时任务
#/var/spool/cron/username ##任务保存文件
1 crontab 时间表示方式
* * * * * ##每分钟
*/2 * * * * ##每两分钟
*/2 09-17 * * * ##早9-晚5 每两分钟
*/2 */2 * * * ##每隔两小时美两分钟
*/2 09-17 3,5 1 5 ##1月的3号5号 每周5 的早9-晚5 每2分钟
*/2 09-17 * * 5 ##每周周五早9 晚5
2 系统控制crontab的服务
crond.service ##当程序开启时定时任务生效
3 crontab
crontab -e -u
crontab -l -u
crontab -r -u
postfix ##发送邮件
mailx ##接收邮件
接收端
[root@westosaaaaaa ~]# systemctl status crond.service ##查看是否开启crond.service
[root@westosaaaaaa ~]# watch -n 1 ls -l /mnt/ ##监控
发送端
[root@westosaaaaaa ~]# crontab -u root -e ##设定一分钟后建立文件rtt
[root@westosaaaaaa ~]# cat /var/spool/cron/root
* * * * * touch /mnt/rtt{1..10}
[root@westosaaaaaa ~]# crontab -l
* * * * * touch /mnt/rtt{1..10}
[root@westosaaaaaa ~]# crontab -u root -e ##设定2分钟后删除文件rtt
crontab: installing new crontab
"/tmp/crontab.hBja2Z":1: bad day-of-week
errors in crontab file, can't install.
Do you want to retry the same edit? y
crontab: installing new crontab
[root@westosaaaaaa ~]# cat /var/spool/cron/root
*/2 * * * * rm -fr /mnt/*
##用户的黑白名单来设定定时任务
[root@westosaaaaaa ~]# vim /etc/cron.deny ##设定用户黑名单为 westos
[root@westosaaaaaa ~]# cat /etc/cron.deny
westos
[root@westosaaaaaa ~]# su - westos ##切换 westos普通用户
Last login: Fri Mar 26 14:04:37 CST 2021 on tty2
[westos@westosaaaaaa ~]$ crontab -e ##执行命令被拒绝
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information
[westos@westosaaaaaa ~]$ exit
logout
[root@westosaaaaaa ~]# vim /etc/cron.allow ##设定用户白名单为 lee
[root@westosaaaaaa ~]# cat /etc/cron.allow
lee
[root@westosaaaaaa ~]# su - westos ##切换用户 westos
Last login: Thu Nov 11 14:57:46 CST 2021 on pts/1
[westos@westosaaaaaa ~]$ crontab -e ##执行命令被拒绝
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information
##系统下临时文件的管理方式
watch -n 1 ls -lR /mnt/ ##监控/mnt/下的文件
[root@westosaaaaaa tmpfiles.d]# vim /lib/tmpfiles.d/westos.conf ##过8秒可以删除
[root@westosaaaaaa tmpfiles.d]# cat /lib/tmpfiles.d/westos.conf
d /mnt/westos 1777 root root 8s
[root@westosaaaaaa tmpfiles.d]# systemd-tmpfiles --create /lib/tmpfiles.d/westos.conf
[root@westosaaaaaa tmpfiles.d]# touch /mnt/westos/westosrtt8
[root@westosaaaaaa tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/westos.conf
[root@westosaaaaaa tmpfiles.d]# vim /etc/cron.d/westos ##整点进行自动删除
[root@westosaaaaaa tmpfiles.d]# cat /etc/cron.d/westos
01 * * * * root systemd-tmpfiles --clean /lib/tmpfiles.d/*.conf
[root@westosaaaaaa tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/westos.conf
[root@westosaaaaaa tmpfiles.d]# touch /mnt/westos/westosrtt12
touch: cannot touch '/mnt/westos/westosrtt12': No such file or directory
[root@westosaaaaaa tmpfiles.d]# systemd-tmpfiles --create /lib/tmpfiles.d/westos.conf
[root@westosaaaaaa tmpfiles.d]# touch /mnt/westos/westosrtt12