延时与定时任务的建立及处理
延时任务at
为更直观看到效果
监视一个 watch -n 1 ls /mnt/ -l
[[email protected] ~]# date c查看时间
[[email protected] ~]# at 21:11 建立延时任务
at> touch /mnt/file{1..5}
at> <EOT> ctrl+d 开启任务
可见在设定的时间,延时命令自动生效
[[email protected] ~]# at -l 查看任务
[[email protected] ~]# at -c 3 查看任务内容
at -r 3 删除任务
at 黑名单
vim /etc/at.deny
at 白名单
vim /etc/at.allow(默认不存在,但若存在,黑名单失效,除此名单之外的普通用户都不能执行at)
定时任务
定时任务与延时任务都可用监视
建立定时任务 crontab -u root -e
####-u 代表用户-e代表建立
(也就是编辑) /var/spool/cron/root
定时任务格式(五个)
分钟 小时 天 月 周
* * * * * ###每分钟一个任务
* 08-17 * * * ##8-17点每分钟一个任务
*/2 08-17 * * * ##8-17点每两分钟一个
*/2 08-17 1,15 * * #每月1,15号8-17点
每两分钟一个任务
*/2 08-17 1,15 3-5 3 #3,4,5月1,15号与周三
8-17点
每两分钟一个任务
15 10 * * * /bin/rm -rf /mnt/*
####每 10:15 清空/mnt/下的内容
时间一到自动清除
定时任务黑名单
vim /etc/cron.deny
提示不可以
超级用户执行crontab -u student -e时,可以执行
白名单
vim /etc/cron.allow
(与延时类似)
白名单建立,黑名单失效
[[email protected] ~]# cd /etc/cron.
cron.d/ cron.daily/ cron.hourly/ cron.monthly/ cron.weekly/
将任务放在以上目录,分别表示每天,每小时,每月,每周执行一次
[[email protected] ~]# cd /etc/cron.d
[[email protected] cron.d]# ls
0hourly raid-check sysstat unbound-anchor
[[email protected] cron.d]# vim westos
###在内写* * * * * root touch /mnt/file{1..10} ####表示每分钟建立文件
[[email protected] cron.d]# cat westos
* * * * * root touch /mnt/file{1..10}
监控一个
watch -n -1 ls -R /mnt/ -l
对比上述两图可发现,每分钟建立文件,时间戳随时间改变
[[email protected] ~]# cd /usr/lib/tmpfiles.d/ 进入目录
[[email protected] tmpfiles.d]# ls
abrt.conf lvm2.conf spice-vdagentd.conf
cups.conf mdadm.conf subscription-manager.conf
cups-lp.conf pam.conf systemd.conf
gvfsd-fuse-tmpfiles.conf ppp.conf systemd-nologin.conf
initscripts.conf rpm.conf tmp.conf
iscsi.conf samba.conf x11.conf
legacy.conf saslauthd.conf
libselinux.conf selinux-policy.conf
[[email protected] tmpfiles.d]# vim westos.conf 建立脚本 清空8s前的
d /mnt/westos 1777 root root 8s
[[email protected] tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/* 建立一个临时目录
[[email protected] tmpfiles.d]# touch /mnt/westos/file 建立个文件
[[email protected] tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* 8s后可删除
[[email protected] tmpfiles.d]# touch /mnt/westos/file 依次建立文件
[[email protected] tmpfiles.d]# touch /mnt/westos/file1
[[email protected] tmpfiles.d]# touch /mnt/westos/file2
[[email protected] tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* 只删除够8s的
[[email protected] tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*