Linux 系统中的定时任务及延时任务

  • 一.系统延时任务及定时任务
  • 1.系统延时任务
  • 2.at任务的黑白名单
  • 3.系统定时任务
  • <1>.crontab 时间表示方式
  • <2>.系统控制crontab的服务
  • <3>.crontab
  • 4.系统级别的cron(文件方式设定定时任务)
  • 5.crontab的黑白名单
  • 4.系统中临时文件的管理方式


一.系统延时任务及定时任务

1.系统延时任务

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

centos 定时任务 执行一次_定时任务

[root@localhost ~]# at now+1min        ##延时1分钟
at> rm -fr /mnt/*
at> <EOT>

centos 定时任务 执行一次_centos 定时任务 执行一次_02

at -l                    				查看任务列表
at -c    任务号                			查看任务内容
at -r    任务号                			取消任务执行

centos 定时任务 执行一次_定时任务_03

centos 定时任务 执行一次_linux_04

centos 定时任务 执行一次_白名单_05

$: 需要 有mailx-12.5-29.el8.x86_64 postfix-2:3.3.1-8.el8.x86_64 才能实现
当任务有输出时,输出会以邮件的形式发送给at任务的发起者

mail -u root         查看超级用户邮件

1 查看第一封邮件
q 退出

> /var/spool/mail/root    			清空邮件

2.at任务的黑白名单

/etc/at.deny        	系统中默认存在,在此文件中出现的用户不能执行at
/etc/at.allow        	系统中默认不存在,当文件出现,普通用户不能执行at
                       	只有在名单中的用户可以,并且/etc/at.deny失效

我们先添加一个用户:

centos 定时任务 执行一次_centos 定时任务 执行一次_06


然后打开黑名单:

centos 定时任务 执行一次_centos 定时任务 执行一次_07

添加用户:

centos 定时任务 执行一次_定时任务_08

切换到这个用户下,再执行at指令:

centos 定时任务 执行一次_运维_09


我们没有执行权力!!

再创建一个白名单:

centos 定时任务 执行一次_白名单_10


centos 定时任务 执行一次_centos 定时任务 执行一次_11


centos 定时任务 执行一次_linux_12

切换到另一个不在白名单的用户,就会发现该用户无法执行at指令:

centos 定时任务 执行一次_运维_13


$:白名单一旦开启,黑名单就失效了,只有在白名单上的用户可以执行at指令。

3.系统定时任务

/var/spool/cron/username    							任务保存文件

<1>.crontab 时间表示方式

* * * * *               ##每分钟
*/2 * * * *            ##每两分钟
*/2 09-17 * * *            ##早7-晚5每两分钟
*/2 */2 * * *            ##每隔2小时每两分钟
*/2 09-17 3,5 1 5        #
*/2 09-17 * * 5            ##每周周五早9晚5

<2>.系统控制crontab的服务

crond.service            						当程序开启时定时任务生效

<3>.crontab

crontab -e -u                    				建立任务
crontab -l -u                      				查看任务
crontab -r -u                      				删除任务

centos 定时任务 执行一次_定时任务_14


centos 定时任务 执行一次_运维_15


centos 定时任务 执行一次_定时任务_16


centos 定时任务 执行一次_linux_17


centos 定时任务 执行一次_运维_18


centos 定时任务 执行一次_centos 定时任务 执行一次_19


centos 定时任务 执行一次_centos 定时任务 执行一次_20

4.系统级别的cron(文件方式设定定时任务)

vim /etc/cron.d/file
* * * * *   username    action
* * * * *   root    rm -fr /mnt/*

监控一下:

centos 定时任务 执行一次_centos 定时任务 执行一次_21


步骤:

centos 定时任务 执行一次_白名单_22

centos 定时任务 执行一次_centos 定时任务 执行一次_23

centos 定时任务 执行一次_运维_24


监控内容清空:

centos 定时任务 执行一次_定时任务_25

5.crontab的黑白名单

/etc/cron.deny        系统中默认存在,在此文件中出现的用户不能执行crontab
/etc/cron.allow        系统中默认不存在,当文件出现,普通用户不能执行crontab                               

                              $:只有在名单中的用户可以,并且/etc/at.deny失效
                              $:这两个名单都不会影响/etc/cron.d/目录中定时任务的发起及执行

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

d /usr/lib/tmpfiles.d/
vim westos.conf
d   /mnt/westos    777 root root 8s
systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*            (3秒之后消失)

centos 定时任务 执行一次_linux_26


centos 定时任务 执行一次_白名单_27


centos 定时任务 执行一次_定时任务_28


centos 定时任务 执行一次_linux_29

centos 定时任务 执行一次_定时任务_30


centos 定时任务 执行一次_linux_31