1.延迟任务
(1)at命令
at 时间 #设定在该时刻要进行什么操作
at now+数字min #在几分钟后要进行什么操作
at -l #查看该操作的编号(只能查看到最新建立但又未执行的操作)
at -c 3 #查看编号为3要做什么操做(操作一旦执行便查看不到了)
at -r 3 #撤回编号为3的操作
实验:
##在/mnt目录下建立目录和文件
[root@localhost ~]# mkdir /mnt/westos
[root@localhost ~]# touch /mnt/westos/file{1..3}
[root@localhost ~]# ls -l /mnt/westos/file{1..3}
[root@localhost ~]# ls -lR /mnt/
监控:
[root@localhost ~]# watch -n 1 ls -lR /mnt/
在另一个shell里:
[root@localhost ~]# date
Mon Nov 5 09:55:07 EST 2018
[root@localhost ~]# at 09:57
at> rm -rf /mnt/westos/file1
at> <EOT> (ctrl+d表示发起任务)
job 1 at Mon Nov 5 09:57:00 2018
##查看任务编号
[root@localhost ~]# at -l
##查看编号为1的具体任务内容
[root@localhost ~]# at -c 1
##查看编号为1的具体任务内容
[root@localhost ~]# at -c 1
[root@localhost ~]# at now+1min
at> poweroff
at> <EOT>
job 2 at Mon Nov 5 10:01:00 2018
[root@localhost ~]# at -l
2 Mon Nov 5 10:01:00 2018 a root
[root@localhost ~]# at -r 2
## -r表示撤销任务
[root@localhost ~]# at -r 2
[root@localhost ~]# at -l
@默认所有用户均能使用at命令发起延迟任务
[root@localhost ~]# su - student
Last login: Mon Nov 5 05:43:17 EST 2018 on pts/0
[student@localhost ~]$ at now+1min
at> echo hello word!
at> <EOT>
job 3 at Mon Nov 5 10:03:00 2018
[student@localhost ~]$ exit
logout
[root@localhost ~]# su - westos
Last login: Sat Nov 3 09:28:33 EDT 2018 on pts/0
[westos@localhost ~]$ at now+1min
at> echo hello word!
at> <EOT>
job 4 at Mon Nov 5 10:03:00 2018
[westos@localhost ~]$ exit
logout
(2).白名单和黑名单的指定
/etc/at.deny ##黑名单
/etc/at.allow ##白名单(默认系统中没有白名单)
注意:白名单和黑名单的指定,仅针对于普通用户;白名单一旦出现,黑名单便会失效。
实验:
@1.默认黑名单存在
[root@localhost ~]# ll /etc/at.deny
-rw-r--r--. 1 root root 1 Jan 29 2014 /etc/at.deny
##将wesots用户添加到黑名单中
[root@localhost ~]# vim /etc/at.deny
################
westos
测试:
[root@localhost ~]# su - westos
Last login: Mon Nov 5 10:02:31 EST 2018 on pts/0
[westos@localhost ~]$ at now+1min
You do not have permission to use at.
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - student
Last login: Mon Nov 5 10:01:58 EST 2018 on pts/0
[student@localhost ~]$ at now+1min
at> echo hello word!
at> <EOT>
job 5 at Mon Nov 5 10:06:00 2018
[student@localhost ~]$ exit
logout
还原:
[root@localhost ~]# vim /etc/at.deny
################
删除 westos
测试:
[root@localhost ~]# su - westos
Last login: Mon Nov 5 10:04:49 EST 2018 on pts/0
[westos@localhost ~]$ at now+1min
at> echo hello word!
at> <EOT>
job 6 at Mon Nov 5 10:10:00 2018
[westos@localhost ~]$ exit
logout
@2.默认白名单不存在
[root@localhost ~]# ll /etc/at.allow
ls: cannot access /etc/at.allow: No such file or directory
##白名单文件一旦建立,则所有用户(root用户除外)均在不能使用at
[root@localhost ~]# touch /etc/at.allow
[root@localhost ~]# su - student
Last login: Mon Nov 5 10:05:06 EST 2018 on pts/0
[student@localhost ~]$ at now+1min
You do not have permission to use at.
[student@localhost ~]$ exit
logout
[root@localhost ~]# su - westos
Last login: Mon Nov 5 10:09:15 EST 2018 on pts/0
[westos@localhost ~]$ at now+1min
You do not have permission to use at.
[westos@localhost ~]$ exit
logout
@将westos用户添加到白名单中
[root@localhost ~]# vim /etc/at.allow
###############
westos
测试:
[root@localhost ~]# su - westos
Last login: Mon Nov 5 10:16:56 EST 2018 on pts/0
[westos@localhost ~]$ at now+1min
at> echo hello word!
at> <EOT>
job 10 at Mon Nov 5 10:18:00 2018
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - student
Last login: Mon Nov 5 10:12:14 EST 2018 on pts/0
[student@localhost ~]$ at now+1min
You do not have permission to use at.
[student@localhost ~]$ exit
logout
@当白名单与黑名单同时存在时,黑名单会失效
[root@localhost ~]# cat /etc/at.allow
westos
[root@localhost ~]# cat /etc/at.deny
westos
测试:
[root@localhost ~]# su - westos
Last login: Mon Nov 5 10:17:24 EST 2018 on pts/0
[westos@localhost ~]$ at now+1min
at> echo hello world!
at> <EOT>
job 11 at Mon Nov 5 10:21:00 2018
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - student
Last login: Mon Nov 5 10:17:51 EST 2018 on pts/0
[student@localhost ~]$ at now+1min
You do not have permission to use at.
[student@localhost ~]$ exit
logout
##查看邮件,可查看到刚发起的echo的内容
[root@localhost ~]# mail
还原:
[root@localhost ~]# vim /etc/at.deny
################
删除westos
[root@localhost ~]# vim /etc/at.allow
###############
删除westos
2.定时任务
crontab -e #以当前用户身份发起定时任务
crontab -u root -e #以root用户身份发起定时任务
crontab -u student -e #以student用户身份发起定时任务
crontab -u root -l #查看root用户的定时任务
crontab -u student -l #查看student用户的定时任务
crontab -u root -r #删除所有定时任务
##查看帮助
[root@localhost ~]# man 5 crontab
任务内容格式详解:
一般格式:时间+任务
时间的具体写法:
分钟 小时 天 月 周
* * * * * #每天每分钟
*/3 08,17 * * * #每天的早上8点和下午的5点每隔3分钟
*/2 08-17 12 6 3 #6月12日早上8 点到下午的5点每隔2分钟和6月的星期3 即:周和天是独立的
(1)用户级的定时任务
##管理定时任务的服务为crond;能发起定时任务的前提是此服务已开启
[root@localhost ~]# systemctl status crond.service
[root@localhost ~]# rm -rf /mnt/*
##建立文件
[root@localhost ~]# touch /mnt/westos{1..5}
##监控(不要退出)
[root@localhost ~]# watch -n 1 ls -l /mnt
在另外一个shell里:
##以root用户身份发起定时任务;可以不加-u默认表示以当前用户身份发起定时任务
整点过一秒便会执行删除命令
[root@localhost ~]# crontab -u root -e
###############
* * * * * rm -rf /mnt/* #每分钟执行一次删除命令
##查看任务;也可以不加-u 表示查看当前用户的任务
[root@localhost ~]# crontab -l
* * * * * rm -rf /mnt/*
[root@localhost ~]# crontab -u root -l
* * * * * rm -rf /mnt/*
##定时任务记录在/var/spool/cron/root文件中
[root@localhost ~]# cat /var/spool/cron/root
* * * * * rm -rf /mnt/*
##-r表示撤销定时任务
[root@localhost ~]# crontab -u root -r
[root@localhost ~]# crontab -u root -l
(2)系统级的定时任务
监控:
[root@localhost ~]# touch /mnt/westos{1..3}
[root@localhost ~]# watch -n 1 ls -l /mnt/
在另一个shell中:
##发起任务
[root@localhost ~]# cd /etc/cron.d
[root@localhost cron.d]# ls
0hourly raid-check sysstat unbound-anchor
##文件名任意,但必须在/etc/cron.d目录下
[root@localhost cron.d]# vim westos
###################
* * * * * root rm -rf /mnt/*
每天每分钟 以root用户身份 删除文件
[root@localhost cron.d]# cat westos
* * * * * root rm -rf /mnt/*
[root@localhost cron]# touch /mnt/file{1..10}
##立即关闭服务,此时不会执行该任务
[root@localhost cron]# systemctl stop crond.service
##开启服务后也不会执行因为时间已经过了;但到下一次还是执行
[root@localhost cron.daily]# systemctl start crond.service
还原:
[root@localhost cron.d]# pwd
/etc/cron.d
[root@localhost cron.d]# rm -rf westos
(3)白名单和黑名单的指定
/etc/cron.deny ##黑名单
/etc/cron.allow ##白名单(系统默认没有白名单)
注意:白名单和黑名单的指定,仅针对于普通用户;白名单一旦出现,黑名单便会失效。
@默认所有用户都可以执行crontab发起定时任务
[root@localhost ~]# su - student
Last login: Sat Nov 3 21:32:15 EDT 2018 on pts/0
##以当前用户身份发起任务;普通用户只能用自己的身份发起任务(即不能加-u)
[student@localhost ~]$ crontab -e
no crontab for student - using an empty one
crontab: installing new crontab
[student@localhost ~]$ exit
logout
[root@localhost ~]# su - westos
Last login: Sat Nov 3 21:32:31 EDT 2018 on pts/0
[westos@localhost ~]$ crontab -e
no crontab for westos - using an empty one
crontab: installing new crontab
[westos@localhost ~]$ exit
Logout
@1.默认黑名单存在
[root@localhost ~]# ll /etc/cron.deny
-rw-------. 1 root root 0 Jan 27 2014 /etc/cron.deny
@将westos用户添加到黑名单中
[root@localhost ~]# vim /etc/cron.deny
[root@localhost ~]# cat /etc/cron.deny
Westos
测试:
[root@localhost ~]# su - westos
Last login: Sat Nov 3 22:21:17 EDT 2018 on pts/0
[westos@localhost ~]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - student
Last login: Sat Nov 3 22:19:44 EDT 2018 on pts/0
[student@localhost ~]$ crontab -e
crontab: installing new crontab
[student@localhost ~]$ exit
Logout
还原:
[root@localhost ~]# vim /etc/cron.deny
[root@localhost ~]# cat /etc/cron.deny
[root@localhost ~]# su - westos
Last login: Mon Nov 5 11:00:24 EST 2018 on pts/0
[westos@localhost ~]$ crontab -e
crontab: no changes made to crontab
[westos@localhost ~]$ exit
Logout
@2.默认白名单不存在
[root@localhost ~]# ll /etc/cron.allow
ls: cannot access /etc/cron.allow: No such file or directory
@白名单一旦建立,则所有(root用户除外)用户均不能用crontab发起定时任务
[root@localhost ~]# touch /etc/cron.allow
[root@localhost ~]# su - student
Last login: Sat Nov 3 22:25:29 EDT 2018 on pts/0
[student@localhost ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
[student@localhost ~]$ exit
logout
[root@localhost ~]# su - westos
Last login: Sat Nov 3 22:26:25 EDT 2018 on pts/0
[westos@localhost ~]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information
[westos@localhost ~]$ exit
logout
@将westos用户加入到白名单中
[root@localhost ~]# vim /etc/cron.allow
[root@localhost ~]# cat /etc/cron.allow
westos
测试:
[root@localhost ~]# su - westos
Last login: Mon Nov 5 11:09:13 EST 2018 on pts/0
[westos@localhost ~]$ crontab -e
crontab: installing new crontab
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - student
Last login: Mon Nov 5 11:09:03 EST 2018 on pts/0
[student@localhost ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
[student@localhost ~]$ exit
logout
@当白名单与黑名单同时存在时,黑名单便会失效
[root@localhost ~]# vim /etc/cron.deny
[root@localhost ~]# cat /etc/cron.deny
westos
[root@localhost ~]# cat /etc/cron.allow
westos
测试:
[root@localhost ~]# su - westos
Last login: Mon Nov 5 11:12:29 EST 2018 on pts/0
[westos@localhost ~]$ crontab -e
crontab: installing new crontab
[westos@localhost ~]$ exit
logout
[root@localhost ~]# su - student
Last login: Mon Nov 5 11:12:42 EST 2018 on pts/0
[student@localhost ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
[student@localhost ~]$ exit
logout
还原:
root@localhost ~]# vim /etc/cron.deny
###############
删除westos
[root@localhost ~]# vim /etc/cron.allow
###############
删除westos
[root@localhost ~]# cat /etc/cron.deny
[root@localhost ~]# cat /etc/cron.allow
3.对临时文件的清理
systemd-tmpfiles --create /usr/lib/tmpfiles.d/* #creat 创建
systemd-tmpfiles --clean /usr/lib/tmpfiles.d/* #clean 清除
实验:
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
[root@localhost mnt]# watch -n 1 ls -lR /mnt
监控:
[root@localhost ~]# watch -n 1 ls -lR /mnt/
在另一个shell中:
[root@localhost cron.d]# cd /usr/lib/tmpfiles.d
[root@localhost tmpfiles.d]# ls
##文件名以.conf结尾即可
[root@localhost tmpfiles.d]# vim westos.conf
######################
d /mnt/westos 777 root root 8s
文件类型 名称 权限 所有人 所有组 存在时间
## --create表示创建;创建/usr/lib/tmpfiles.d/目录下的westos.conf文件里的任务
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# touch /mnt/westos/file1
##等待8秒再建立file2
[root@localhost tmpfiles.d]# touch /mnt/westos/file2
##立刻执行,清理临时文件;此时只有file1被清理调,因为file1已经存在了超过8秒
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*