at和crontab两种工作调度

at 和 cron是两种工作调度
(1)例行性:每隔一定的周期执行任务
(2)突发性:这次任务完成后,就没有的那一种
at 是可以处理仅执行一次就结束调度的命令。at这个命令来生成所要运行的工作,并将这个工作以文本的方式写入/var/spool/at目录里
at 使用下来的文件来对at进行使用限制
at 17:00          在17:00时,延迟并执行某一个任务
at now+2min       在两分钟后,执行某个任务
at -l             显示正在执行的任务
at -c 工作号码    列出当前工作号码执行的任务
at -r 工作号码    撤销当前工作号码执行的任务
练习
[root@localhost ~]# date           #查看当前日期
2018年 04月 24日 星期二 07:53:28 EDT
[root@localhost ~]# at 7:55        #在7:55时执行任务
at> touch /mnt/file{1..5}          #在/mnt/下建立file1到file5个文件
at> <EOT>                          #按ctrl+d执行任务
job 3 at Tue Apr 24 07:55:00 2018
[root@localhost ~]# at -c 3        #列出正在执行的详情
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=1; export XDG_SESSION_ID
HOSTNAME=localhost; export HOSTNAME
SHELL=/bin/bash; export SHELL
........
[root@localhost ~]# at -l          #显示正在执行的任务
3    Tue Apr 24 07:55:00 2018 a root
[root@localhost ~]# at  -r 3       #撤销当前正在执行的任务
[root@localhost ~]# at now+2min    #两分钟后时执行任务
at> touch  /mnt/www                #在/mnt/下建立www文件
at> <EOT>                          #按ctrl+d执行任务
job 4 at Tue Apr 24 07:56:00 2018
[root@localhost ~]# at -l          #列出当前执行的任务
4    Tue Apr 24 07:56:00 2018 a root

at.allow  白名单,写入白名单里,白名单里的用户可以执行at命令,
其他用户不可以执行
at.deny   黑名单,写入黑名单,黑名单里的用户不可以执行at命令,其他用户可以执行
若同时存在黑名单和白名单,那么忽略黑名单存在,只考虑白名单
若黑名单和白名单都不存在,那么只有root用户可以使用at这个命令


crontab 定时任务
crontab  用于设置周期性被执行的指令执行
cron 系统调度进程,可以使用它在每天的非高峰负荷时间段运行工作,或在一周或一月的不同时间段运行,这个可以是由我们自行设定,cron可以在我们无需人工干预的情况下完成定时任务
-u:只有root才能进行这个任务,也即帮其他用户新建/删除crontab工作调度
-e:编辑crontab的工作内容
-l:查阅crontab的工作内容
-r:删除所有的crontab的工作内容,若只要删除一项,请用-e去编辑
crontab -u root -e  #发起定时任务
crontab -u root -l  #查看定时任务
crontab -u root -r  #删除正在执行的任务
cron.daily          #每天执行一次任务
cron.weekly         #每星期执行一次任务
cron.monthly        #每月执行一次任务
cron.hourly         #每小时执行一次任务
cron.d              #系统自动定期需要做的任务
crontab             #设定定时任务执行文件
cron.deny           #定时命令的黑名单,如果某个用户在这个名单里,则这个用户不能执行这个crontab命令
cron.allow          #定时命令的白名单,如果某个用户在这个名单里,则这个用户能执行这个crontab命令,其他用户不能执行此命令,但是超级用户除外
如果同时执行有黑白名单,那么黑名单就不重要了,取决于白名单。
*   代表任何时刻
,  代表分隔时段
-   代表一段时间范围内
/n  每隔n单位间隔执行一次任务
*     *     *    *   *  #分钟 小时 天 月 周
*    08-17  *    *   *  #每天的8点到17点发起定时任务
*/2  08-17  *    *   *  #每天的8点到17点每隔两分钟发起定时任务
*/2  08-17 1,15  *   *  #每月的第一天和第15天的8点到17点每隔两分钟发起一个任务
*/2  08-17 1,15 3-5  *  #每年的3到5月的的第一天和第15天的8点到17点每隔两分钟发起一个任务
   
*/2  08-17 1,15 3-5  3  #每年的3到5月的第三周的第一天和第15天的8点到17点每隔两分钟发起一个任务
练习
ot@localhost Desktop]# crontab -u root -e  #发起定时任务
crontab: installing new crontab

截图


[root@localhost Desktop]# crontab -u root -l  #查看正在执行的任务
16 9 * * * touch /mnt/file{1..5}
截图crontab监控


[root@localhost Desktop]# crontab -u root -e

no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost Desktop]# crontab -u root -l  #查看正在执行的任务
20  9 * * * touch /mnt/file
[root@localhost Desktop]# crontab -u root -r  #删除正在执行的任务,对已有的任务不起任何作用。

systemd-tmpfiles  --create /usr/lib/tmpfiles.d/* #创建临时文件
vim  westos.conf  编辑配置文件
systemd-tmpfiles  --clean  /usr/lib/tmpfiles.d/* #删除临时文件
练习
[root@localhost tmpfiles.d]# vim westos.conf
[root@localhost tmpfiles.d]# pwd
/usr/lib/tmpfiles.d
[root@localhost tmpfiles.d]# vim westos.conf
[root@localhost tmpfiles.d]# cat westos.conf
d  /mnt/westos 1777 root  root 10s
[root@localhost tmpfiles.d]# touch /mnt/westos/file
[root@localhost tmpfiles.d]# systemtemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
bash: systemtemd-tmpfiles: command not found...
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# touch /mnt/westos/file
[root@localhost tmpfiles.d]# touch /mnt/westos/file1
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
[root@localhost tmpfiles.d]# touch /mnt/westos/file1
[root@localhost tmpfiles.d]# rm -rf /mnt/file*
[root@localhost tmpfiles.d]# touch /mnt/westos/file
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*

猜你喜欢

转载自blog.csdn.net/dhjibk/article/details/80090752