延迟任务(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
如果我们不需要这些输出,想要将这些输出屏蔽掉,有以下三种方法:
-
修改输出邮件指向
-
编辑crontab任务,在任务内容前加上
MAILTO=''
使邮件发送到空 -
编辑
/etc/crontab
,将邮件指向改为空MAILTO=''
##此配置在系统启动时已经读取过,修改只能在下次启动后生效
-
-
关闭邮件功能
修改
/etc/sysconfig/crond
配置,彻底关闭邮件功能 -
将输出重定向
编辑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
*************************************************************
##每两小时过两分钟清理一次临时文件