一、延时任务
监控命令:watch -n -1 ls -l /mnt/
[root@localhost ~]# at 05:39 定时时间
at> rm -rf /mnt/file1 动作
at> <EOT> ctrl+d 启动任务
[root@localhost ~]# at -l 查看任务
[root@localhost ~]# at -c3 查看任务详细内容
[root@localhost ~]# at -r 删除任务(加数字删除对应任务)
[root@localhost ~]# at now+1min 一分钟后
at> touch /mnt/file1 执行动作
1、黑白名单:
黑名单:出现在这个名单的用户被禁止使用定时任务
[root@localhost ~]# ls -l /etc/at.deny
2、白名单:
需要自己建立,有了白名单则黑名单失效,只有白名单用户可以使用定时任务
[root@localhost ~]# touch /etc/at.allow
定时任务 可以同时运行N个任务
二、定时任务
1、以 root 用户执行次任务
[root@localhost ~]# crontab -u root -e
查看任务
[root@localhost pub]# crontab -l
* * * * * rm -rf /mnt/*
分 时 天 月 周
删除任务
[root@localhost pub]# crontab -u root -r
* * * * * rm -rf /mnt/file{1,3,5}
每分 每小时 每天 每月 每个星期
*/2 09,22 4,15 11 7 rm -rf /mnt/file{2,4,6}
每2分钟 09点和22点 4号和15号 11月 每周天执行
*/2 09-22 4-15 11 7 rm -rf /mnt/file{7,8,9,10}
每2分钟 09点到22点 4号到15号 11月 每周天执行
2、黑白名单
自己建立定时白名单:建立后所有用户不能使用定时任务(默认允许)除白名单用户
[root@localhost ~]# touch /etc/cron.allow
添加用户
[root@localhost pub]# vim /etc/cron.deny
[root@localhost pub]# vim /etc/cron.allow
白名单添加tom后
[westos@localhost ~]$ crontab -e
用root可以使白名单未添加的用户使用定时任务
[root@localhost pub]# crontab -u student -e
no crontab for student - using an empty one
crontab: installing new crontab
三、系统级
1、切换目录:
[root@localhost ~]# cd /etc/cron.d
[root@localhost cron.d]# ls
0hourly raid-check sysstat unbound-anchor
2、编辑添加目录
[root@localhost cron.d]# vim westos
添加定时任务:必须添加执行用户
* * * * * root rm -rf /mnt/*
程序:关闭后不会执行定时任务
systemctl status crond.service
2、临时文件清理
1、切换目录
[root@localhost mnt]# cd /usr/lib/tmpfiles.d/
2、建立目录
[root@localhost tmpfiles.d]# vim westos.conf
添加
d /mnt/westos 777 root root 10s
3、自动生成文件westos
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
4、扫描文件
systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
5、给文件建立文件
[root@localhost tmpfiles.d]# touch /mnt/westos/file2
6、再次扫描(设定时间过去后)
systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*