linux下的定时与延时任务

                                          linux下的定时与延时任务

一、延迟任务
1.延迟任务的发起

at 10:10          执行时间
rm -fr /mnt/*    执行内容
>[ctrl]+[d]         任务发起
at -l               查看任务队列 
at -c 任务号   查看任务内容
at -r 任务号    取消任务

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
2.当延迟任务有输出,输出会以邮件形式发送到任务发起者邮箱中
环境搭建:

[root@rhel8_node2 ~]# dnf whatprovides */mail

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试:

at 10:30 timedatectl 此命令有输出但不会显示在字符设备中 此命令输出会以邮件防守发送给at发起用户
[root@rhel8_node2 ~]# at 15:05 
warning: commands will be executed using /bin/sh
at> timedatectl<EOT>
job 1 at Mon Feb 24 15:05:00 2020

在这里插入图片描述
3.at命令的控制
/etc/at.deny 执行 at命令的用户黑名单,名单中的人不能执行at
/etc/at.allow 此文件默认不存在,
**注意:**当/etc/at.allow 此文件存在/etc/at.deny不生效 并且当此文件存在系统普通用户默认不能执行at,只有在名单中 的人可以执行at命令。

[root@rhel8_node2 ~]# touch /etc/at.allow
[root@rhel8_node2 ~]# vim /etc/at.
at.allow  at.deny   

在这里插入图片描述二、定时任务

1.crond 设定方式

命令设定方式:
用户级别定时任务

crontab -u root -e    设定
 crontab -u root -l   查看 
 crontab -u root -r   删除 
 /var/spool/cron/root 任务存储位置(用户名为名称的文件)
[root@rhel8_node2 ~]# crontab -u root -e  回车后会进入编辑模式
crontab: installing new crontab

[root@rhel8_node2 ~]# cat /var/spool/cron/root
56     15    24    02    01      rm -fr /mnt/*
分钟   小时   日期   月    周一       命令

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

任务:系统命令或脚本

0和7都代表星期天
 分钟     小时   天   月   周                                             
08-17      *     *    *   *            每天每小时08分-17分 
08-17      *     *    *   3,5          每周3和周五每小时08分-17分 
08-17/2    *     *    *   3,5          每周3和周五每小时08分-17分时间段每隔2分钟
08-17/2   10     *    *   3,5          每周3和周五10点08分-17分时间段每4隔2分钟 
08-17/2   10     5    *   3,5          每周3和周五及每月5号10点08分-17分时间段每隔2分钟
08-17/2   10     5    3   3,5         3月每周3和周五及3月5号10点08分-17分时间段每隔2分钟

2.配置文件方式设定:

系统级别的cron,只有超级用户可操作
[root@rhel8_node2 ~]# ls -ld /etc/cron.d/
drwxr-xr-x. 2 root root 51 Feb 24 16:27 /etc/cron.d/

在这里插入图片描述
在这里插入图片描述

在相应的目录下创建文件书写命令
/etc/cron.daily/       每天任务 
/etc/cron.hourly/      每小时任务 
/etc/cron.monthly/     每月任务
 /etc/cron.weekly/     每周执行动作

3.用户级别crond控制设定

vim /etc/cron.deny     cron用户名单,用法与at.deny 一样
vim /etc/cron.allow     cron白名单,用法与at.allow  一样

在这里插入图片描述

发布了46 篇原创文章 · 获赞 6 · 访问量 1340

猜你喜欢

转载自blog.csdn.net/qq_46089299/article/details/104477137