1.延迟任务at(延迟任务是暂时的)
(1)延迟任务的设定
at + 时间 # 在什么时候干什么事情
at -l # 查看延迟任务的编号
at -c [任务号] # 查看延迟任务的详细内容
at -r [任务号] # 撤销要执行的延迟任务
ctrl +d # 发起任务
[root@localhost Desktop]# dateSun Nov 4 23:42:05 CST 2018 [root@localhost Desktop]# at 23:43
at> touch /mnt/file
at> <EOT>
job 5 at Sun Nov 4 23:43:00 2018
[root@localhost Desktop]# at -l
3 Mon Nov 5 23:40:00 2018 a root
5 Sun Nov 4 23:43:00 2018 a root
[root@localhost Desktop]# at -c 5
[root@localhost mnt]# watch -n 1 ls -l /mnt # 重新打开一个shell,监控/mnt目录
[root@localhost Desktop]# at now+1min # 一分钟后要干的事情
at> echo hello
at> <EOT>job 6 at Sun Nov 4 23:48:00 2018
[root@localhost Desktop]# mail # 在邮件里可以看到执行的结果
(2)取消定时任务
[root@localhost Desktop]# date
Sun Nov 4 23:52:40 CST 2018
[root@localhost Desktop]# at 23:53
at> touch /mnt/file{1..3}
at> <EOT>
job 7 at Sun Nov 4 23:53:00 2018
[root@localhost Desktop]# at -l
3 Mon Nov 5 23:40:00 2018 a root
[root@localhost Desktop]# at -r 3 # 3是查看到的进程号
[root@localhost Desktop]# at -l # 再此查看任务时发现任务被取消,查看不到
(3)延迟任务的黑白名单
/etc/at.deny # 黑名单
/etc/at.allow # 白名单
[root@localhost Desktop]# ls -l /etc/at.deny # 默认黑名单存在
-rw-r--r--. 1 root root 1 Jun 22 2015 /etc/at.deny
You have mail in /var/spool/mail/root
[root@localhost Desktop]# ls -l /etc/at.allow # 白名单不存在
ls: cannot access /etc/at.allow: No such file or directory
当我们手动建立白名单时,黑名单就立刻失效,当用户同时出现在黑名单和白名单中时,生效的是白名单
[root@localhost Desktop]# vim /etc/at.deny
1 westos[root@localhost Desktop]# su - westos
[westos@localhost ~]$ at now+1min # westos用户在黑名单中,不能设定延迟任务
You do not have permission to use at.
[westos@localhost ~]$ logout
[root@localhost Desktop]# su - linux
Last login: Sat Nov 3 10:43:48 CST 2018 on pts/0
[linux@localhost ~]$ at now+1min # linux用户不在黑名单中,可以执行
at> touch /mnt/file
at> <EOT>
job 12 at Mon Nov 5 00:11:00 2018
但是我发现,普通用户根本不能发起延迟任务
[root@localhost ~]# mail # 查看邮件发现被拒绝
让我们来查找一下原因
[root@localhost Desktop]# ll -d /mnt # 查看/mnt目录的权限时,发现/mnt目录对其它用户不可写
drwxr-xr-x. 2 root root 6 Nov 4 23:58 /mnt
[root@localhost Desktop]# chmod 777 /mnt
# 再次发起延迟任务,成功
2.定时任务(永久)
crontab命令用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于
“crontab”文件中,以供之后读取和执行。 cron 系统调度进程。 可以使用它在每天的非高
峰负荷时间段运行作业,或在一周或一月中的不同时段运行。cron是系统主要的调度进程,可
以在无需人工干预的情况下运行作业。 crontab命令允许用户提交、编辑或删除相应的作业。
每一个用户都可以有一个crontab文件来保存调度信息。系统管理员可以通过cron.deny 和
cron.allow 这两个文件来禁止或允许。
用户拥有自己的crontab文件。
(1)crontab的用法
crontab -u(指定用户) root(所要指定的用户) -e # 发起任务,以超级用户的身份编辑任务
分钟 小时 天 月 周
* * * * * touch /mnt/file # 每分钟
* 08-17 * * * touch /mnt/file # 8-17点的每分钟
* 08,17 * * * touch /mnt/file # 8和17点的每分钟
*/2 08-17 * * * touch /mnt/file # 8-17点的每两分钟
*/2 08-17 1,15 * * touch /mnt/file # 每月的第一和第十五天的8-17点的每两分钟
*/2 08-17 1,15 * 3 touch /mnt/ # 每月的第一和第十五天和每周三的8-17点的每两分钟
*/2 08-17 1,15 3,5 3 touch /mnt/file # 每三和五月的第一和第十五天和每周三的8-17点的每两分钟
[root@localhost Desktop]# crontab -e # 直接发起定时任务,以哪个用户身份登陆,就默认以哪个用户发起任务
1 * * * * * touch /mnt/file{1..3}
[root@localhost Desktop]# crontab -l # 查看任务
* * * * * touch /mnt/file{1..3}
[root@localhost Desktop]# cat /var/spool/cron/root # 实际上任务是被写在了这个文件里,是以哪个用户发起的,就会自动生成以哪个用户命名的文件
* * * * * touch /mnt/file{1..3}
[root@localhost Desktop]# watch -n 1 ls -l /mnt # 监控/mnt目录查看任务是否执行成功
[root@localhost Desktop]# crontab -r # 撤销任务
[root@localhost Desktop]# crontab -l # 再次查看没有任务
no crontab for root
[root@localhost Desktop]# crontab -u student -e # 指定用户发起定时任务
[root@localhost Desktop]# crontab -u student -l
1 */2 * * * * touch /mnt/student{1..3}
[root@localhost Desktop]# cat /var/spool/cron/student # 所有用户crontab文件存放的目录,以用户名命名
1 */2 * * * * touch /mnt/student{1..3}
[root@localhost Desktop]# watch -n 1 ls -l /mnt
(2)crontab的黑白名单
/etc/cron.deny # 定时任务黑名单
/etc/cron.allow # 定时任务白名单
当白名单存在时,黑名单失效,若白名单存在且为空,则所有的用户将是黑名单
[root@localhost Desktop]# vim /etc/cron.deny 1 westos
[root@localhost Desktop]# su - westos
[westos@localhost ~]$ crontab -e
[root@localhost Desktop]# vim /etc/cron.allow # westos用户同时存在于黑名单和白名单中,可见生效的是白名单
1 westos
[root@localhost Desktop]# su - westos
Last login: Sun Nov 4 10:30:06 CST 2018 on pts/1
[westos@localhost ~]$ crontab -e
1 * * * * * rm -fr /mnt/*
[westos@localhost ~]$ crontab -l
* * * * * rm -fr /mnt/*
[root@localhost Desktop]# vim /etc/cron.deny # linux用户在黑名单里
1 linux
[root@localhost Desktop]# crontab -u linux -e # 但是依旧可以发起定时任务,是因为它是由root用户发起的 1
* * * * * rm -fr /mnt/*
[root@localhost Desktop]# crontab -u linux -l
* * * * * rm -fr /mnt/*
[root@localhost Desktop]# watch -n 1 ls -l /mnt
(3)系统中的crontab的定制
[root@localhost Desktop]# ls /etc/cron # 按tab键可显示出来,它们都是cron的调度配置文件
cron.allow cron.daily/ cron.hourly/ crontab cron.d/ cron.deny cron.monthly/ cron.weekly/
cron.daily是每天执行一次的任务
cron.weekly是每个星期执行一次的任务
cron.monthly是每月执行一次的任务
cron.hourly是每个小时执行一次的任务
cron.d是系统自动定期需要做的任务
crontab是设定定时任务执行文件
cron.deny文件就是用于控制不让哪些用户使用
crontab的功能
[root@localhost Desktop]# vim /etc/cron.d/minute # 文件里需要指定用户
1 * * * * * root touch /mnt/file{1..3}
[root@localhost ~]# watch -n 1 ls -l /mnt
注:定时任务的进程是由crond进行控制的,如果此服务没有安装或者服务没有开启,则定时任务无法执行
注:做完实验记得清除所有的定时任务[root@localhost tmpfiles.d]# crontab -u student -l
*/2 * * * * touch /mnt/student{1..3}
[root@localhost tmpfiles.d]# crontab -u student -r
[root@localhost tmpfiles.d]# crontab -u westos -l
* * * * * rm -fr /mnt/*
[root@localhost tmpfiles.d]# crontab -u westos -r
[root@localhost tmpfiles.d]# crontab -r
[root@localhost tmpfiles.d]# crontab -u linux -r
(4)定时任务的清理(把定时任务写在临时文件里)
系统中的临时文件系统中服务在正常运行时会产生临时文件,在系统中/usr/lib/tmpfiles.d中,它是服务的临时文件的存放位置
systemd-tmpfiles # 创建、删除和管理临时文件的服务
清理临时文件(设定生命周期来清理)
[root@localhost Desktop]# cd /usr/lib/tmpfiles.d/
[root@localhost tmpfiles.d]# vim clean.conf
1 d /mnt/westos 777 root root 10s
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/* # 执行命令,创建上面脚本中的/mnt/westos目录
[root@localhost tmpfiles.d]# touch /mnt/westos/westosfile1
[root@localhost tmpfiles.d]# touch /mnt/westos/westosfile2 # 隔几秒之后再次新建一个文件
[root@localhost Desktop]# watch -n 1 ls -lR /mnt # 监控命令
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* # 清除在/mnt/westos里存在时间超过设定时间10s的文件
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* # 再次执行命令,westosfile2就会被删除,则目录被清空
[root@localhost Desktop]# watch -n 1 ls -lR /mnt # 监控命令
[root@localhost tmpfiles.d]# rm -fr /usr/lib/tmpfiles.d/clean.conf # 做完实验记得还原环境