2.4 系统定时任务与延迟任务

延迟任务(at)

[root@Song Desktop]# date					#查看当前时间
Wed Jul 21 11:40:50 CST 2021
[root@Song Desktop]# at 11:45				#设置一个延迟任务
warning: commands will be executed using /bin/sh
at> touch {1..5}
at> <EOT>
job 2 at Wed Jul 21 11:45:00 2021
[root@Song Desktop]# at -l					#查看延迟任务列表
2	Wed Jul 21 11:45:00 2021 a root
[root@Song Desktop]# at -c 2				#查看任务详细内容
[root@Song Desktop]# at -r 2				#取消任务

at任务的黑白名单

/etc/at.deny								#系统中默认存在,此文件中出现的用户不能执行at
/etc/at.allow								#系统中默认不存在,只有超级用户和此文件中出现的用户才能执行at并且/etc/at.deny失效

注意:/etc/at.allow文件存在并且为空时/etc/at.deny不失效

定时任务(cron)

服务名称:crond.service

当服务开启时定时任务才能生效

crontab设置定时任务

crontab 
		-e	#编辑用户的crontab
		-l	#列出用户的crontab
		-r	#删除用户的crontab
		-u	#指定用户
crontab -e -u song							#编辑用户song的crontab
*************************************************************
* * * * * touch /mnt/file{1..5}
分钟小时天月周

crontab建立的定时任务保存在/var/spool/cron/username

crontab黑白名单

/etc/cron.deny								#系统中默认存在,此文件中出现的用户不能执行crontab
/etc/cron.allow								#系统中默认不存在,只有超级用户和此文件中出现的用户才能执行crontab并且/etc/cron.deny失效

注意:/etc/cron.allow文件存在并且为空时/etc/cron.deny不失效

任务输出处理

at及cron的任务在执行中会出现有输出的任务,默认情况下系统会将输出以邮件形式发送至任务发起者的邮箱/var/spool/mail/username

如果我们不需要这些输出,想要将这些输出屏蔽掉,有以下三种方法:

  1. 修改输出邮件指向

    • 编辑crontab任务,在任务内容前加上MAILTO=''使邮件发送到空

      在这里插入图片描述

    • 编辑/etc/crontab,将邮件指向改为空MAILTO=''

      在这里插入图片描述

      ##此配置在系统启动时已经读取过,修改只能在下次启动后生效

  2. 关闭邮件功能

    修改/etc/sysconfig/crond配置,彻底关闭邮件功能

    在这里插入图片描述

  3. 将输出重定向

    编辑crontab任务,在任务最后加上输出重定向指令,使输出指向/dev/null

    在这里插入图片描述

系统级定时任务

系统临时文件管理

系统临时文件保存路径:/usr/lib/temfiles.d/

新建一个配置文件song.conf

vim /usr/lib/tmpfiles.d/song.conf
*************************************************************
d	/mnt/song	1777	root	root	10s
*************************************************************
##表示/mnt/song/目录下的文件最短存在时间是10s

使该配置文件生效:

systemd-temfiles --create /lib/temfiles.d/song.conf

生效后文件夹/mnt/song就变成了一个临时文件目录,里面的文件都是都被系统认定为临时文件。

在目录/mnt/song下间隔10s建立两个文件file1和file2后立即执行以下命令:

systemd-temfiles --clean /lib/temfiles.d/song.conf

file1会被清除掉,file2不会被清除,因为file1存在时间大于10s,file2小于10s。

系统级定时任务

系统级定时任务目录:/etc/cron.d/

##任务格式:
vim /etc/cron.d/file
*************************************************************
* * * * *	username	action
* * * * *	root		date
*************************************************************

crontab的黑白名单不会影响系统级定时任务

新建一个系统级定时任务song:

vim /etc/cron.d/song
*************************************************************
2 */2 * * * root systemd-temfiles --clean /lib/temfiles.d/*.conf
*************************************************************
##每两小时过两分钟清理一次临时文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值