RHCE--复习(一)之系统延迟任务及定时任务

系统延迟任务及定时任务

  #关闭图形命令(第二次开启的时候)
  [root@localhost ~]# systemctl set-default multi-user.target
  
  #windos系统级别类型  0-6个级别
  #查看级别类型
  [root@localhost ~]# runlevel 

一、延迟任务

  • 在系统中我们的维护工作大多数时在服务器行对闲置时进行

  • 我们需要用延迟任务来解决自动进行的一次性的维护

  • 延迟任务时一次性的,不会重复执行

  • 当延迟任务产生输出后,这些输出会以邮件的形式发送给延迟任务发起者

  • 在RHEL9中默认系统中的所有普通用户都可以发起延时任务

1.1延迟任务的发起

在系统中我们可以使用at命令来发起延迟任务 at命令执行是调用的是atd服务,即使系统最小化安装atd也会被安装到系统中at 任务信息存放在系统中/var/spool/at目录中at 任务的日志文件被存放到/var/log/cron中at 任务执行时如果遇到系统处于关闭状态,那么在开启时atd任务会检测未执行的任务并且立即执行 延迟任务发起方法如下:

环境搭建

#发起延迟任务
[root@rhce-fuxi ~]# at now+2min
warning: commands will be executed using /bin/sh
at> rm -fr /mnt/easylee/*  #输入延迟任务
at> <EOT>               # ctrl+d 启动延迟任务
job 1 at Wed Sep 11 21:39:00 2024
[root@rhce-fuxi ~]#

1.2 at命令参数

  • at 命令执行格式为 at 参数

  • -m 当任务完成之后,即使没有标准输出,将给用户发送邮件

  • -l atg的别名,可列出目前系统上面的所有该用户的at调度

  • -d atrm的别名,可以取消一个在at调度中的工作

  • -v 使用较明显的时间格式,列出qt调度中的任务列表

  • -c 可以列出后面接的该项工作的实际命令内容

  • -f 从文件中读取作作业

#安装邮件服务和邮件服务客户端
[root@rhce-fuxi ~]# dnf install postfix s-nail-14.9.22-6.el9.x86_64 -y
#启动
[root@rhce-fuxi ~]# systemctl enable --now postfix.service
#发送邮件和查看邮件

1.3 at 命令的时间表示方法

  • HH:MM 在今天的HH小时MM分钟执行,如果今天的这个时间点已经过了,则明天执行。

  • HH:MM YYYY-MM-DD 强制规定在某年某月的某一天的特殊时刻进行该工作

  • now+2 minutes 从现在开始几分钟minutes,hours, days, or weeks

1.4 at非交互方式执行

[root@rhce-fuxi mnt]# vim test.sh
[root@rhce-fuxi mnt]# sh test.sh
warning: commands will be executed using /bin/sh
job 6 at Wed Sep 11 22:12:00 2024
[root@rhce-fuxi mnt]# at -l
6       Wed Sep 11 22:12:00 2024 a root
[root@rhce-fuxi mnt]# at -c 6

1.5 延迟任务的黑白名单

超级用户可以执行at,普通用户默认情况可以执行

[root@rhce-fuxi mnt]# useradd user1
您在 /var/spool/mail/root 中有新邮件
[root@rhce-fuxi mnt]# useradd user2
[root@rhce-fuxi mnt]# su - user1
[user1@rhce-fuxi ~]# at now+1min
warning: commands will be executed using /bin/sh
at> <EOT>
job 7 at Wed Sep 11 22:14:00 2024
[user1@rhce-fuxi ~]#

缺乏安全性,解决方法如下,限制用户的执行

[user1@rhce-fuxi mnt]# man at.deny

#用户黑名单
[root@rhce-fuxi mnt]# vim /etc/at.deny
[root@rhce-fuxi mnt]# cat /etc/at.deny
user1
[root@rhce-fuxi mnt]#

#此时user1写不了
[user1@rhce-fuxi ~]$ at now+1min
You do not have permission to use at.
[user1@rhce-fuxi ~]$


#有白名单后 ,user2不能执行
[root@rhce-fuxi mnt]# touch /etc/at.allow
[root@rhce-fuxi mnt]# cat /etc/at.allow
[root@rhce-fuxi mnt]#

[user2@rhce-fuxi ~]$ at now+1min
You do not have permission to use at.
[user2@rhce-fuxi ~]$


#把user1写入白名单
[root@rhce-fuxi mnt]# vim /etc/at.allow
[root@rhce-fuxi mnt]# cat /etc/at.allow
user1
[root@rhce-fuxi mnt]# su - user1
[user1@rhce-fuxi ~]$ at now+1min
warning: commands will be executed using /bin/sh
at> <EOT>
job 10 at Wed Sep 11 22:43:00 2024
[user1@rhce-fuxi ~]$ exit
注销
[root@rhce-fuxi mnt]# su - user2
[user2@rhce-fuxi ~]$ at now+1min
You do not have permission to use at.
[user2@rhce-fuxi ~]$

二、定时任务

使用服务 crond.service

2.1.定时任务介绍

  • 和延迟任务类似,我们可以用crontab来解决自动进行的周期性的维护

  • 定时任务类似手机闹钟,会重复执行

  • 当定时任务产生输出后,这些输出会以邮件的形式发送给延迟任务发起者。

  • 在RHEL9中默认系统中的所有普通用户都可以发起定时任务

2.2定时任务的发起

  • 在系统中我们可以使用crontab命令来发起用户级定时

  • 任务在系统中/etc/cronfilename是系统级定时任务,只有root用户可以发起。

  • 定时任务执行是调用的是crond服务,即使系统最小化安装crond也会被安装到系统中

  • 定时任务信息存放在系统中/var/spool/cron目录中,存放文件和发起人名称一致

  • 定时任务的日志文件被存放到/var/log/cron中

  • 定时任务执行时如果遇到系统处于关闭状态,那么在开启时crond任务会检测未执行的任务并且立即执行

2.3发起用户级周期行任务

[root@rhce-fuxi ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@rhce-fuxi ~]# crontab -l
* * * * * rm -fr /mnt/test.sh
[root@rhce-fuxi ~]#
#  * * * * *   对应  分  时  天  月  周
#在创建后面也会删除
[root@rhce-fuxi ~]# touch /mnt/test.sh

2.4定时任务的时间表示

#查看时间表示
[root@rhce-fuxi ~]# man 5 crontab

  minute         0-59
  hour           0-23
  day of month   1-31
  month          1-12 (or names, see below)
  day of week    0-7 (0 or 7 is Sunday, or use names)


* * * * *    每分钟
*/2 * * * *  每两分钟
*/2 09,17 * * *  9点和下午5点每两分钟执行一次
*/2 09-17 * * *  9点 到下午5点每两分钟执行一次
*/2 09-17 10,15 * *  每月10号和每月15号早上9点到下午5点每两分钟执行一次
*/2 09-17 10,15 3 *  3月10号和3月15号早上9点到下午5点每两分钟执行一次
*/2 09-17 10,15 3 5   3月的每个周五、 3月的10号和3月15号早上9点到下午5点每两分钟执行一次

2.5用户级定时任务的发起

  1. 周期任务发起方式 crontab命令的执行格式为: crontab-u用户-e

  1. crontab命令参数如下

  • -u :用来设定某个用户的crontab服务此参数一般有root用户来运行

  • file: file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab

  • -e :编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件

  • -l :显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。

  • -r :从/var/spool/cron目录中删除某个用户的crontab文件

  • -i :在删除用户的crontab文件时给确认提示。

  • crontab 在设定定时任务是会以vi命令打开临时文件在保存时会把内容同步到/var/spool/cron中

  • 文件格式为:分 时 天 月 周 执行动作 1111 * * * echo hello #每天11点11分执行echo hello 命令

    时间表达方式

#参数演示

#发起并指定
[root@rhce-fuxi mnt]# crontab -u user1 -e
no crontab for user1 - using an empty one
crontab: installing new crontab
[root@rhce-fuxi mnt]# ls -l /var/spool/cron/user1
-rw------- 1 root root 15  9月 13 14:30 /var/spool/cron/user1
[root@rhce-fuxi mnt]#

[root@rhce-fuxi mnt]# crontab  -l -u user1
* * * * * date

[root@rhce-fuxi mnt]# crontab -r -i -u user1
crontab: really delete user1's crontab?

 

2.6 发起系统级别的周期任务

系统级别crontab目录详解

2.7定时任务执行优化

  • 定时任务在执行时首先会查看执行者是否名单中

  • 系统中默认只存在黑名单/etc/cron.deny,写入此文件中的用户会被禁止使用定时任务。

  • 在系统中可以手动建立白名单/etc/cron.allow,当此文件出现黑名单不会被检测

  • 白名单出现只有root用户和白名单用户可以执行定时任务

  • 如果系统中没有黑白名单那么只有root用户可以执行任务

  • 黑白名单为用户列表,每个用户一行

  • 黑白名单只针对用户级定时任务进行限制

[root@rhce-fuxi ~]# ll /etc/cron.deny
-rw-r--r--. 1 root root 0 11月 30  2023 /etc/cron.deny
[root@rhce-fuxi ~]# ll /etc/cron.allow


[root@rhce-fuxi ~]# vim /etc/cron.deny
[root@rhce-fuxi ~]# cat /etc/cron.deny
user1

[root@rhce-fuxi ~]# su - user1
[user1@rhce-fuxi ~]$ crontab -e
You (user1) are not allowed to use this program (crontab)
See crontab(1) for more information
[user1@rhce-fuxi ~]$


#创建白名单
[root@rhce-fuxi ~]# touch /etc/cron.allow
[root@rhce-fuxi ~]# cat /etc/cron.allow
[root@rhce-fuxi ~]#

此时普通用户都不可以

[root@rhce-fuxi ~]# echo user1 > /etc/cron.allow
[root@rhce-fuxi ~]#

[user1@rhce-fuxi ~]$ crontab -l
* * * * * date
[user1@rhce-fuxi ~]$

#超级用户可以使用,无权限限制
[root@rhce-fuxi ~]# crontab -u user2 -e

#限制超级用户的
[root@rhce-fuxi mnt]# chattr +i easylee/
[root@rhce-fuxi mnt]# ls
!  easylee  hgfs
[root@rhce-fuxi mnt]# rm -fr easylee/
rm: 无法删除 'easylee/': 不允许的操作
[root@rhce-fuxi mnt]#

#查看
[root@rhce-fuxi mnt]# lsattr -d easylee/
----i----------------- easylee/
[root@rhce-fuxi mnt]# chattr -i easylee/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值