一.在系统中设定延迟任务要求如下
在系统中建立easylee用户,设定其密码为easylee
延迟任务由root用户建立
要求在5小时后备份系统中的用户信息文件到/backup中
确保延迟任务是使用非交互模式建立
确保系统中只有root用户和easylee用户可以执行延迟任务的设定
(1)创建用户
[root@server200 ~]# useradd easylee
[root@server200 ~]# passwd easylee
Changing password for user easylee.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
(2)创建延迟任务
非交互式,写入脚本中,运行脚本
[root@server200 ~]# vim set.sh
[root@server200 ~]# cat set.sh
at now+5hours <</upup
cat /etc/passwd 1>/backup
cat /etc/shadow 1>>/backup
/upup
[root@server200 ~]# sh set.sh
warning: commands will be executed using /bin/sh
job 20 at Wed Apr 17 16:13:00 2024
[root@server200 ~]# at -l
23 Wed Apr 17 16:30:00 2024 a root
[root@server200 ~]# cat /backup
cat: /backup: No such file or directory
等待五小时后阅读文件来查看。用户信息写入/backup文件中
(3)设置白名单
[root@server200 ~]# vim /etc/at.allow
[root@server200 ~]# cat /etc/at.allow
easylee
将用户esaylee写入白名单中。
有了白名单,只有写入白名单中的用户与超级管理员有权限来设置延迟任务。
[root@server200 ~]# su easylee
[easylee@server200 root]$ at now+1min
warning: commands will be executed using /bin/sh
at> <EOT>
job 25 at Thu Apr 18 18:52:00 2024
[easylee@server200 root]$ su user1
[user1@server200 root]$ at now+1min
You do not have permission to use at.
二.在系统中设定定时任务并优化定时任务
每天凌晨4点显示系统的根设备使用信息并以邮件的形式发送到root邮箱中
确保此任务为系统定时任务
周期性任务:
[root@server200 ~]# vim /etc/cron.d/job
[root@server200 ~]# cat /etc/cron.d/job
* 04 * * * root df -h | mail -s test root
df命令查看磁盘空间使用情况
注意要提前开启邮件服务
dnf install postfix s-nail-14.9.22-6.el9.x84_64 -y
#安装邮件服务
systemctl enable --now postfix
#开启邮件服务