• linux系统定时任务与延迟任务


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



    一、延迟任务(一次性)

    比如:今天设置了延迟任务,今天下午清理文件,延迟任务执行了,明天就不会执行,除非再设置一次


    1.1 环境

    [root@trade test1]# touch yan{1..3} 
    [root@trade test1]# ls -l
    total 0
    -rw-r--r--. 1 root root 0 Aug 29 10:50 yan1
    -rw-r--r--. 1 root root 0 Aug 29 10:50 yan2
    -rw-r--r--. 1 root root 0 Aug 29 10:50 yan3
    [root@trade test1]# watch -n 1 ls -l /root/test1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    监控

    在这里插入图片描述


    1.2 创建延迟任务

    开启新窗口,在x时间清空掉/root/test1的文件

    ctrl+d任务发起

    [root@trade ~]# at 11:00
    at> rm -fr /root/test1/*
    
    • 1
    • 2

    ctrl+d生效后

    at> <EOT>
    job 1 at 2022-08-29 11:00
    
    • 1
    • 2

    11:00:00准时清除掉该目录下的文件

    在这里插入图片描述

    多个任务,一行一行写,写完ctrl +d


    或者这样写
    单位也可以是1day

    [root@trade ~]# at now+1min
    at> rm -fr /root/test1/*
    
    • 1
    • 2
    at> <EOT>
    job 6 at 2022-08-29 13:21
    
    • 1
    • 2

    在这里插入图片描述


    1.3 查看任务列表

    [root@trade ~]# at 11:05
    at> touch /root/test1/ywq
    at> <EOT>
    job 3 at 2022-08-29 11:05
    
    • 1
    • 2
    • 3
    • 4
    [root@trade ~]# at -l
    3	2022-08-29 11:05 a root
    
    • 1
    • 2

    1.4 查看任务内容

    很长,看最后一行,就知道任务内容

    [root@trade ~]# at -c 3
    
    • 1

    在这里插入图片描述

    at -l查到任务05分执行,20分时任务执行完成了

    在这里插入图片描述


    1.5取消任务内容

    [root@trade ~]# at -l
    5	2022-08-29 13:30 a root
    
    • 1
    • 2

    新版本这样取消

    [root@trade ~]# at -r 5
    
    • 1

    老版本这样取消

    [root@trade ~]# atrm 5
    
    • 1
    [root@trade ~]# at -l
    [root@trade ~]# 
    
    • 1
    • 2

    二、at命令黑白名单

    默认任意用户都可以使用at命令
    这样首先安全性不高,其次任意用户发起at命令会使得系统负载变高

    系统中默认存在黑名单

    [root@trade ~]# ls -l /etc/at.deny
    -rw-r--r--. 1 root root 1 Jan 20  2012 /etc/at.deny
    [root@trade ~]# ls -l /etc/at.allow
    ls: cannot access /etc/at.allow: No such file or directory
    
    • 1
    • 2
    • 3
    • 4

    写法

    [root@trade ~]# man 5 at.deny 
    
    • 1

    有at.allow的时候,at.deny不生效

    在这里插入图片描述


    黑名单

    [root@trade ~]# su - yan
    [yan@trade ~]$ at 13:40
    at> ^C[yan@trade ~]$ logout
    
    [root@trade ~]# vim /etc/at.deny 
    [root@trade ~]# cat /etc/at.deny 
    yan
    
    [root@trade ~]# su - yan
    [yan@trade ~]$ at 13:40
    You do not have permission to use at.
    [yan@trade ~]$ logout
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    [root@trade ~]# vim /etc/at.deny 
    [root@trade ~]# cat /etc/at.deny 
    [root@trade ~]# su - yan
    [yan@trade ~]$ at 13:40
    at> ^C
    
    • 1
    • 2
    • 3
    • 4
    • 5

    白名单

    有白名单,黑名单不生效

    除了白名单的用户和超级用户可以使用at命令以外,所有用户都不能使用at命令

    [root@trade ~]# touch /etc/at.allow
    [root@trade ~]# su - oracle
    [oracle@trade ~]$ at now+1min
    You do not have permission to use at.
    [oracle@trade ~]$ logout
    
    [root@trade ~]# su - yan
    [yan@trade ~]$ at now+1min
    You do not have permission to use at.
    [yan@trade ~]$ logout
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    [root@trade ~]# vim /etc/at.allow 
    [root@trade ~]# cat /etc/at.allow 
    oracle
    
    [root@trade ~]# su - oracle
    [oracle@trade ~]$ at now+1min
    at> ^C[oracle@trade ~]$ logout
    
    [root@trade ~]# su - yan
    [yan@trade ~]$ at now+1min
    You do not have permission to use at.
    [yan@trade ~]$ logout
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    三、用户级的定时任务(永久)

    保证crond这个服务是开启的(默认情况下是开的)

    rhel6

    [root@trade ~]# service crond status
    crond (pid  21661) is running...
    
    • 1
    • 2

    rhel7后

    systemctl status crond.service
    
    • 1

    写法:

    man 5 crontab
    
    • 1

    0和7都表示周天
    分钟 小时 天 月 周

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


    -u指定用户,-e编辑
    回车会就可以直接编辑了

    [root@trade ~]# crontab -u root -e
    
    • 1

    在这里插入图片描述


    每分钟

    * * * * * rm -fr /root/test1/*
    
    • 1

    用户是你用什么用户写的crontab任务

    wq保存后,保存到/var/spool/cron/用户

    [root@trade ~]# cat /var/spool/cron/root 
    * * * * * rm -fr /root/test1/*
    
    • 1
    • 2

    查看定时任务

    [root@trade ~]# crontab -u root -l
    * * * * * rm -fr /root/test1/*
    
    • 1
    • 2
    [root@trade ~]# touch /root/test1/yan{1..3}
    
    • 1

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


    撤销掉用户的所有定时任务

    [root@trade ~]# crontab -u root -r
    [root@trade ~]# crontab -u root -l
    no crontab for root
    
    • 1
    • 2
    • 3

    删掉的是这个文件

    [root@trade ~]# ls /var/spool/cron/
    [root@trade ~]# 
    
    • 1
    • 2

    每隔2min(分钟)

    */2 * * * * rm -fr /root/test1/*
    
    • 1

    早上9点到下午5点(小时)

    */2 09-17 * * * rm -fr /root/test1/*
    
    • 1

    每月1号到5号(天)

    */2 09-17 1,5 * * rm -fr /root/test1/* 
    
    • 1

    只有4月(月)

    */2 09-17 1,5 4 * rm -fr /root/test1/* 
    
    • 1

    只有周一(星期)

    4月的1号和5号和4月的每一个周一早上09点到下午5点每隔2min执行一次

    */2 09-17 1,5 4 1 rm -fr /root/test1/*  
    
    • 1

    [root@trade ~]# crontab -u yan -e
    [root@trade ~]# ls /var/spool/cron/
    root  yan
    
    • 1
    • 2
    • 3

    [root@trade ~]# crontab -u root -e
    crontab: installing new crontab
    [root@trade ~]# crontab -u root -l
    * * * * * touch /root/test1/yan{1..3}
    [root@trade ~]# service crond stop
    Stopping crond:                                            [  OK  ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述


    [root@trade ~]# service crond start
    Starting crond:                                            [  OK  ]
    
    • 1
    • 2

    在这里插入图片描述

    每min创建一次,当有了就不会创建,这个是上min创建的

    在这里插入图片描述


    四、用户级crontab的执行限制(黑白名单)

    黑白名单不会影响超级用户和系统级crontab

    黑白名单其实和at是一样的

    默认情况下所有用户都可以使用crontab

    [root@trade ~]# su - oracle
    [oracle@trade ~]$ crontab -e
    [root@trade ~]# su - yan   
    [yan@trade ~]$ crontab -e
    
    • 1
    • 2
    • 3
    • 4

    黑名单

    [root@trade ~]# vim /etc/cron.deny 
    [root@trade ~]# cat /etc/cron.deny 
    oracle
    
    • 1
    • 2
    • 3
    [root@trade ~]# su - oracle
    [oracle@trade ~]$ crontab -e
    You (oracle) are not allowed to use this program (crontab)
    See crontab(1) for more information
    
    • 1
    • 2
    • 3
    • 4

    但是用超级用户下用oracle执行crontab可以写

    [root@trade ~]# crontab -u oracle -e
    
    • 1
    [root@trade ~]# crontab -u oracle -l
    * * * * * touch /home/oracle/yan{1..3}
    
    • 1
    • 2

    在这里插入图片描述


    默认情况下是没有白名单

    [root@trade ~]# ls -l /etc/cron.allow
    ls: cannot access /etc/cron.allow: No such file or directory
    
    • 1
    • 2

    白名单

    有白名单,黑名单就不生效
    只有root和白名单的用户允许,其它都不允许

    [root@trade ~]# su - yan
    [yan@trade ~]$ crontab -e
    crontab: installing new crontab
    [yan@trade ~]$ logout
    [root@trade ~]# su - oracle
    [oracle@trade ~]$ crontab -e
    crontab: installing new crontab
    [oracle@trade ~]$ logout
    
    [root@trade ~]# touch /etc/cron.allow
    [root@trade ~]# echo yan > /etc/cron.allow
    [oracle@trade ~]$ cat /etc/cron.allow 
    yan
    
    [root@trade ~]# su - yan
    [yan@trade ~]$ crontab -e
    crontab: installing new crontab
    [yan@trade ~]$ logout
    
    [root@trade ~]# su - oracle
    [oracle@trade ~]$ crontab -e
    You (oracle) are not allowed to use this program (crontab)
    See crontab(1) for more information
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    五、处理任务输出

    at命令和crontab执行程序,有输出的程序,输出会以邮件的形式发送邮件的发起者
    比如root发起at命令,在系统会生成以root身份给root发邮件


    安装邮件服务
    postfix发邮件 mailx查看邮件

    开启postfix服务后就可以用mail查看邮件

    dnf install postfix mailx -y
    systemctl enable --now postfix
    
    • 1
    • 2

    没有邮件

    [root@server2 ~]# mail -uroot
    No mail for root
    
    • 1
    • 2

    用mail给root发邮件
    Subject标题
    正文
    正文出现.
    然后回车,邮件就发送了

    [root@server2 ~]# mail root
    Subject: test
    adafa
    fsfes
    faf
    。
    .
    EOT
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    mail -u用户 查看用户的文件,查看的时候输入数字,这个邮件内容显示出来

    在这里插入图片描述

    邮件其它指令help
    delete删掉邮件

    在这里插入图片描述

    & delete 1
    & quit
    [root@server2 ~]# mail -uroot
    No mail for root
    
    
    • 1
    • 2
    • 3
    • 4
    • 5

    快速删掉邮件,把邮件文件清空掉
    邮件文件在/var/spool/mail清空那个用户写那个用户

    [root@server2 ~]# > /var/spool/mail/root 
    
    • 1
    [root@server2 ~]# crontab -u root -e
    * * * * * /bin/date
    
    
    • 1
    • 2
    • 3

    默认情况会发送给root
    在这里插入图片描述

    这个文件写了邮件发送给谁

    [root@server2 ~]# vim /etc/crontab 
    
    • 1

    在这里插入图片描述

    如果不想给任何人发邮件,设定为空
    这个文件是系统文件,重启服务不行systemctl restart crond.service

    在这里插入图片描述

    方法二
    立即生效

    [root@server2 ~]# crontab -u root -e
    MAILTO=''
    * * * * * /bin/date
    
    • 1
    • 2
    • 3
    [root@server2 ~]# mail
    No mail for root
    
    
    • 1
    • 2
    • 3

    把MAILTO删掉,配置文件还原
    这样它还是会发送文件

    彻底取消邮件发送功能

    [root@server2 ~]# vim /etc/sysconfig/crond 
    
    
    • 1
    • 2

    -m off 邮件功能关闭
    在这里插入图片描述

    [root@server2 ~]# systemctl restart crond.service 
    
    • 1

    执行了,mail 没有邮件

    [root@server2 ~]# cat /var/log/cron
    Aug 30 21:58:01 server2 CROND[3986]: (oracle) CMD (/bin/date)
    
    
    • 1
    • 2
    • 3
    [root@server2 ~]# mail
    No mail for root
    
    
    • 1
    • 2
    • 3

    更多参数

    [root@server2 ~]# man crond
    
    
    • 1
    • 2

    在这里插入图片描述

    处理有输出的走向,但是无论如何处理它的输出都会在/var/log/cron产生
    在这里插入图片描述

    不想产生,把其输出屏蔽掉,把输出重定向

    [root@server2 ~]# crontab -u oracle -e
    * * * * * /bin/date &> /dev/null
    
    • 1
    • 2

    屏蔽邮件,但是屏蔽邮件后会有日志,彻底把指令输出屏蔽,编写crontab的时候,在执行指令最后加上输出重定向,让它执行指令的时候不要产生输出,彻底杜绝输出的出来


    六、系统级cron的设定

    系统级定时任务只有超级用户可以做

    [root@trade cron.d]# ls -ld /etc/cron.d
    drwxr-xr-x. 2 root root 4096 Aug 29 16:19 /etc/cron.d
    
    • 1
    • 2

    一般用来对临时文件管理

    每天执行的动作,每小时执行的动作,每月执行的动作,每周执行的动作
    在这里插入图片描述
    写个脚本,只需要将该脚本移到上面的目录,就会每天、小时、月、周执行一遍脚本

    我把写的脚本移动/etc/cron.hourly下,那么我的脚本每小时就能执行一次

    [root@trade test]# vim test.sh
    [root@trade test]# cat test.sh 
    #!/bin/bash
    rm -fr /root/test1/*
    
    • 1
    • 2
    • 3
    • 4
    [root@trade test]# cp test.sh /etc/cron.hourly/
    [root@trade test]# chmod +x /etc/cron.hourly/test.sh
    
    • 1
    • 2

    如果想自己定义时间
    在这里插入图片描述
    每次执行都是整点过1分,原因
    前面写的crontab都是整点过1分,比如写的*****,12.00.01才执行

    [root@trade test]# cd /etc/cron.d       
    [root@trade cron.d]# cat 0hourly 
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    HOME=/
    01 * * * * root run-parts /etc/cron.hourly
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    自己写一个
    每分钟以超级用户的身份删掉/root/test1底下的内容

    和用户级crontab相比配置文件多了用户身份

    [root@trade cron.d]# vim yan
    [root@trade cron.d]# cat yan
    * * * * * root rm -fr /root/test1/*
    
    • 1
    • 2
    • 3

    [root@trade oracle]# touch /root/test1/yan{1..3}
    
    • 1

    在这里插入图片描述
    执行成功

    在这里插入图片描述


    七、利用cron优化临时文件存储

    程序在使用临时文件,把临时文件处理掉,程序会出问题
    (比如,你在餐馆吃饭,碗筷类似临时文件,你没吃完,就不给用碗筷)
    临时文件有生命周期,在系统存在x时间后才能被清理
    临时目录/tmp,这里面的文件不会随便消失,存在10天以上会被清理掉
    在这里插入图片描述因为系统中对临时管理文件是有策略
    /lib/tmpfiles.d/目录中全是临时文件的配置
    里面有tmp.conf
    第一列类型 第二列名称 权限 所有人 所有组 生命周期,能看见/tmp里面的存在的东西超过10天会被清理掉

    在这里插入图片描述


    将上个实验的环境删掉

    [root@trade tmp]# cd /etc/cron.d/
    [root@trade cron.d]# rm -fr yan 
    
    • 1
    • 2

    自己写一个
    类型 生成文件名称 权限 所有人 所有组 临时文件生命周期
    自己写必须.conf结尾
    5s后这个临时目录里面的东西才能清理

    [root@server2 tmpfiles.d]# vim /lib/tmpfiles.d/yan.conf
    [root@server2 tmpfiles.d]# cat yan.conf 
    d /mnt/yan 1777 root root 5s
    
    • 1
    • 2
    • 3

    建立用create 清理用clean
    systemd-tmpfiles必须是超级用户才能使用

    root@server2 tmpfiles.d]# systemd-tmpfiles --create /lib/tmpfiles.d/yan.conf 
    
    • 1
    [root@server2 mnt]# watch -n1 ls -l /mnt/yan
    
    • 1
    [root@server2 tmpfiles.d]# touch /mnt/yan/yan{1..3}
    
    • 1

    在这里插入图片描述

    [root@server2 tmpfiles.d]# touch /mnt/yan/yan{3..5}
    
    • 1

    在这里插入图片描述

    [root@server2 tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/yan.conf 
    
    • 1

    在这里插入图片描述

    没有过5s

    [root@server2 tmpfiles.d]# touch /mnt/yan/yan{3..5}
    [root@server2 tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/yan.conf 
    
    • 1
    • 2

    在这里插入图片描述

    5s以后

    [root@server2 tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/yan.conf 
    
    • 1

    在这里插入图片描述


    每s/H自己去清理下系统的临时文件(符合生命周期完成的临时文件),用rm可能对系统造成损害,所以使用临时文件清理指令更温和点
    systemd-tmpfiles必须是超级用户才能使用

    [root@server2 tmpfiles.d]# vim /etc/cron.d/yan
    [root@server2 tmpfiles.d]# cat /etc/cron.d/yan 
    * * * * * root system-tmpfiles --clean /lib/tmpfiles.d/*.conf
    01 * * * *  
    
    • 1
    • 2
    • 3
    • 4
    [root@server2 tmpfiles.d]# touch /mnt/yan/yan{3..5}
    
    • 1

    在这里插入图片描述

    1min后

    在这里插入图片描述


    直接写crontab也行(效果一样)

    [root@server2 cron.d]# rm -fr yan 
    [root@server2 cron.d]# ls
    0hourly
    
    • 1
    • 2
    • 3

    每min清理掉临时文件(符合生命周期完成的临时文件)

    [root@server2 ~]# crontab -u root -e 
    * * * * *  systemd-tmpfiles --clean /lib/tmpfiles.d/*.conf
    
    • 1
    • 2

    END

  • 相关阅读:
    【C++之数组与指针】随机输入整数存入数组并用指针遍历
    python-flask 登录案例
    磁盘分区和加载
    CRC在线计算器
    消息队列实现进程间通信
    入门力扣自学笔记119 C++ (题目编号640)
    2023湖南工商大学计算机考研信息汇总
    经典面试题-小于N的最大数
    智慧园区信息化管理系统发展现状及难题
    The Grapes NFT 概览与数据分析
  • 原文地址:https://blog.csdn.net/westos_yanzheng/article/details/126579121