jobs
1.延时:
at 时间 #在该时间要进行操作at now+5min #在5分钟后要进行
at 16:00
>touch /mnt/file{1..3} #要做的操作
>ctrl+d(按)
at -l #查看该操作的pid
at -c 3 #查看3为什么操作;即要干什么
at -r 3 #删除该操作
vim /etc/at.deny #编辑at配置文件,指定黑名单用户不能执行at操作
vim /etc/at.allow #白名单出现,黑名单失效,只有白名单用户和超级
用户才能执行at操作
2.定时任务crontab
1.命令行执行定时任务:
[root@localhost ~]# crontab -u root -e
crontab: installing new crontab #建立root用户定时任务
分钟(0-60) 小时(0-23) 天(0-31) 月(1-12) 周(0-7)
* * * * * #表每一天的每一分钟
* 08-17 * * *
#表每天的8:00-17:00的每一分钟
*/2 08-17 1,15 * 3
#表每隔两分钟在每个月的1号,15号,以及每个周三的8:00到17:00
* 08-17 1,15 3-5 3
#表在3-5月的每个周三和1,15号在8:00到17:00每分钟
[root@localhost ~]# crontab -u root -l #查看root用户定时任务
* * * * * rm -f /mnt/*
*/2 * * * * touch /mnt/file{1..3}
[root@localhost ~]# crontab -u root -r #删除root执行的定时任务
[root@localhost ~]# vim /etc/cron.deny
linux
[root@localhost ~]# su - linux
[linux@localhost ~]$ crontab -e
You (linux) are not allowed to use this program (crontab)
See crontab(1) for more information
[root@localhost ~]# crontab -u linux -e #允许,因为是超级指定linux去执行cron
[root@localhost ~]# vim /etc/cron.allow #cron白名单出现,黑名单失效
[root@localhost ~]# su - linux
[linux@localhost ~]$ crontab -e #可以执行
2.在/etc/cron.d/目录下面执行定时任务
[root@localhost ~]# cd /etc/cron.d#系统级的
[root@localhost cron.d]# ls
0hourly raid-check sysstat unbound-anchor
[root@localhost cron.d]# vim westos
[root@localhost cron.d]# cat westos
01 * * * * root touch /mnt/file{1..5} #每一分钟的01秒建立/mnt/文件
#cat /var/spool/cron/root #查看root用户要执行的定时任务,用户级的定时任务目录
[root@node2 mnt]# cat /var/spool/cron/root
01 * * * * touch /mnt/file{1..5}
3.临时定时任务的管理
[root@localhost cron.d]# cd /usr/lib/tmpfiles.d/
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/* #创建
[root@localhost tmpfiles.d]# vim westos.conf
d /mnt/westos 1777 root root 8s
类型 名称 权限 所有人 所在组 存在时间
#在/mnt/westos目录下的文件必须在建立8s之后才能被clean清理
[root@localhost tmpfiles.d]# mkdir /mnt/westos
[root@localhost tmpfiles.d]# touch /mnt/westos/file
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*#清理
[root@localhost tmpfiles.d]# cat westos.conf
d /mnt/westos 1777 root root 8s
[root@node2 tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@node2 tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost mnt]# watch -n 1 ls -lR /mnt/ -l #监控