1、系统延时任务
1、[root@localhost ~]# at 21:30 设定任务执行时间
warning: commands will be executed using /bin/sh
2、at> rm -fr /mnt/* 执行删除任务
3、回车
4、ctrl+D 发起任务
at> <EOT>
job 7 at Mon Sep 13 21:40:00 2021
[root@localhost mnt]# at now+1min 表示1min后执行
warning: commands will be executed using /bin/sh
at> rm -fr /mnt/* 执行删除动作
at> <EOT>
job 10 at Mon Sep 13 21:59:00 2021
[root@localhost mnt]# at -l
10 Mon Sep 13 21:59:00 2021 a root
命令 | 功能 |
---|---|
at -l | 查看任务列表 |
at -c 任务号 | 查看任务内容 |
at -r 任务号 | 取消任务执行 |
[root@localhost mnt]# at -l 查看任务列表
7 Mon Sep 13 21:40:00 2021 a root
[root@localhost mnt]# at -c 7 查看任务内容
${SHELL:-/bin/sh} << 'marcinDELIMITER3eb843e9'
rm -fr /mnt/*
[root@localhost mnt]# at -l
8 Tue Sep 14 21:49:00 2021 a root
[root@localhost mnt]# at -r 8 取消任务执行
[root@localhost mnt]# at -l 任务列表以为空
注意:当执行任务命令有输出时,(如date命令)输出不会显示出来,而是以邮件的形式发给at任务的发起者
如和在系统中发邮件?
[root@localhost mnt]# dnf install postfix mailx -y 安装mailx邮件客户端 和postfix邮件投递机制
[root@localhost mnt]# systemctl enable --now postfix.service 开启服务
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service.
命令 | 功能 |
---|---|
mail - u root | 查看用户的邮件 |
[root@localhost mnt]# mail -u root 查看用户的邮件
No mail for root
/var/spool/mail 表示邮箱地址
[root@localhost mail]# ls
rpc westos
[root@localhost mail]# > /var/spool/mail/root 表示清空root用户邮件
测试:当执行有输出的命令时,不会显示出来,而是以发邮件的形式发给at任务的发起者
[root@localhost mail]# at now+2min
warning: commands will be executed using /bin/sh
at> date 执行date命令
at> <EOT>
job 11 at Mon Sep 13 22:16:00 2021
[root@localhost mail]# at -l
11 Mon Sep 13 22:16:00 2021 a root
[root@localhost mail]# mail -u root 查看root用户邮件,已经有了
Heirloom Mail version 12.5 7/5/10. Type ? for help.
"/var/mail/root": 1 message 1 new
>N 1 root Mon Sep 13 22:16 14/524 "Output from your job 1"
& headers 输入headers表示回到邮件队列
& delete 1 删除对列邮件用 delete + 邮件号
& headers
No applicable messages
quit 退出
at命令的执行控制
/etc/at.deny 系统中默认存在,在此文件中的用户不能执行at命令
/etc/at.allow 系统中默认不存在,当文件存在普通用户不能执行at,只有在名单中的用户可以,并且/etc/at.deny 实效
root@localhost ~]# useradd lee 新建用户
[lee@localhost root]$ at 10:08 新建用户也可以执行at命令
warning: commands will be executed using /bin/sh
at>
lee@localhost root]$ vim /etc/at.deny 在这个文建里写的用户,表示禁止使用at命令
lee
[lee@localhost root]$ at 10:23
You do not have permission to use at.
[root@localhost ~]# touch /etc/at.allow 当这个文件被建立后,所有用户at命令会被禁用,只有at.allow 名单里的用户才能使用at命令,at.allow文件被建立后at.deny里的文件内容就不会生效了
[root@localhost ~]# useradd linux
[root@localhost ~]# su -- linux linux用户被禁用at命令
[linux@localhost root]$ at 10:33
You do not have permission to use at.
[westos@localhost root]$ at 10:35 westos用户被禁用at命令
You do not have permission to use at.
[root@localhost ~]# vim /etc/at.allow 在at.allow 文件里加入lee用户
lee
[root@localhost ~]# su -- lee 切换lee用户
[lee@localhost root]$ at 10:39 lee用户可以执行at命令
warning: commands will be executed using /bin/sh
at>
2、系统定时任务
1、系统控制crontab的服务
crond.service 当服务开始时定时任务生效
2、crontab 时间表示方式
* * * * * | 分钟、小时、天、月、周 |
---|---|
*/2 * * * * | 每两分钟 |
*/2 09-17 * * * | 早上9点到晚上5点每两分钟 |
*/2 */2 * * * | 每隔两小时每两分钟 |
3、用户级设定定时任务方式(命令方式)
命令 | 功能 |
---|---|
crontab -e -u root | 表示用root用户发起定时任务, -e表示发起,-u表示用户 |
crontab -l -u root | 表示可以看到用户的定时任务 |
crontab -r -u root | 表示取消用户定时任务 |
root@localhost ~]# crontab -e -u root 发起root用户定时任务
/2 * * * * rm -fr /mnt/*
[root@localhost ~]# crontab -l -u root 查看任务
*/2 * * * * rm -fr /mnt/*
t@localhost cron.d]# crontab -r -u root 删除用户定时任务
4、系统级设定定时任务方式(文件方式设定定时任务)
[root@localhost cron]# cd /etc/cron.d/ 指定目录里新建文件,设定定时任务
[root@localhost cron.d]# ls
0hourly raid-check
[root@localhost cron.d]# vim westos
*/2 * * * * root touch /mnt/lee{1..5} 需要加上用户
5、cd/var/spool/cron/ 任务存放的位置
[root@localhost cron.d]# cd /var/spool/cron/
[root@localhost cron]# ls
root
[root@localhost cron]# cat root
*/2 * * * * rm -fr /mnt/*
注意:命令方式和文件方式都可设定用户定时任务
6、crontab的黑白名单
/etc/cron.deny 系统中默认存在,在此文件中的用户不能执行crontab命令
/etc/cron.allow 系统中默认不存在,当文件存在,普通用户不能执行crontab,只有在名单中的用户可以,并且/etc/at.deny 失效
root@localhost cron.d]# vim /etc/cron.deny 文件里面的用户不能执行crontab命令
lee
[lee@localhost cron.d]$ crontab -e lee用户不能执行crontab命令
You (lee) are not allowed to use this program (crontab)
See crontab(1) for more information
root@localhost cron.d]# touch /etc/cron.allow 当建立cron.allow文件,除了超级用户所有的用户都不能执行crontab命令
[root@localhost cron.d]# su -- westos
[westos@localhost cron.d]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information
[linux@localhost cron.d]$ crontab -e
You (linux) are not allowed to use this program (crontab)
See crontab(1) for more information
[root@localhost cron.d]# vim /etc/cron.allow 添加用户
lee
[root@localhost cron.d]# su -- lee
[lee@localhost cron.d]$ crontab -e crontab命令可以执行
3、系统中临时文件的管理方式
[root@localhost ~]# cd /lib/tmpfiles.d/
[root@localhost tmpfiles.d]# vim westos.conf 编写临时文件
d /mnt/westos 1777 root root 8s 8s 表示临时文件周期为8s
[root@localhost tmpfiles.d]# systemd-tmpfiles --create /lib/tmpfiles.d/westos.conf 创建临时文件
[root@localhost tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/westos.conf 清理临时文件,只有过了临时文件周期才能清理