linux之系统中的延时任务及定时任务

1.系统中的延时任务

[root@localhost ~]# at 23:37 ##设定任务执行时间
at> rm -fr /mnt/* ##任务动作
at> ##用ctrl +D发起任务

[root@localhost ~]# at now+1min ##延时一分钟
at> rm -fr /mnt/*
at>
这里写图片描述
at -l ##查看任务列表
at -c 任务号 ##查看任务内容
at -r 任务号 ##取消任务执行
这里写图片描述
这里写图片描述
这里写图片描述
注意:当任务有输出时,输出会以邮件的方式发送给at任务的发起者
[root@localhost ~]# at now+1min ##延时一分钟
at> echo hello
at>

mail -u root ##查看超级用户文件
1 ##查看第一封邮件
q ##退出

『> /var/spool/mail/root ##清空邮件』
这里写图片描述


2设置任务黑白名单

vim /etc/at.deny ##黑名单 系统中默认存在,在此文件中的用户 不能执行at
vim /etc/at.allow ##白名单 系统默认不存在,当文件出现,普通用户不能执行,只有在名单中的用户可以执行,并且黑名单失效
这里写图片描述


3.系统定时任务

#1.crontab 时间表示

eg:* * * * * rm -fr /mnt/* ##每分钟
* /2 * * * ##每隔两分钟
*/2 09-17 * * ##早7–晚5每两隔分钟
*/2 /2 * * * ##每隔两小时每隔两分钟
*/2 09-17 3,5 1 5 ##三月和五月的一号和三月和五月的周五的早9–晚5的每隔两分钟
*/2 09-17 * 5 ##每周周五早9–晚5的每隔两分钟
05 * * * * ##每小时整点过五分钟
05 02 * * * ##每天2:05am
30 08 01 * * ##每月第一天8:30am
00 07 25 12 * ##12月25 日7:00am
30 16 * * 5 ##每个星期五4:30pm
0 0 1 jan 0 一月一日午夜和一月的每个星期日

#2.系统控制crontab的服务

crond.service ##当服务开启时定时任务生效
systemctl stop crond.service

#3.crontab

crontab -e -u root ##编写文件的内容
crontab -l -u root ##列出文件的内容
crontab -r -u root ##删除文件的内容
直接输入* * * * * rm -fr /mnt/*
这里写图片描述

#4.文件方式设定定时任务 crontab

vim /etc/cron.d/file(root)
* * * * * username action
示例:
* * * * * root rm -fr /mnt/*
这里写图片描述
无界面交互:
echo “* * * * * root rm -fr /mnt/*” > /etc/cron.d/westos

这里写图片描述

#5.crontab的黑白名单

vim /etc/cron.deny ##黑名单 系统中默认存在,在此文件中的用户 不能执行crontab
vim /etc/cron.allow ##白名单 系统默认不存在,当文件出现,普通用户不能执行,只有在名单中的用户和root用户可以执行,并且/etc/cron.deny失效
##这两个名单都不会影响/etc/cron.d/目录中定时任务的发起及执行

这里写图片描述
这里写图片描述


4.系统中临时文件的管理方式

cd /usr/lib/tmpfiles.d
vim westos.conf
d /mnt/westos 777 root root 8s

systemd-tmpfiles –create /usr/lib/tmpfiles.d/* ##建立临时文件/mnt/westos
systemd-tmpfiles –clean /usr/lib/tmpfiles.d/*
这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_37048504/article/details/81330434