(十四)时间延时任务及定时任务

前言

我们常常在工作和学习中会遇到一些需要延时或者定时的任务;比如定时发送文件;定时更新;又或者延时关机等等,这些问题的处理方法我们这一节都会学习

一、延时任务

1.系统延时任务

[root@localhost ~]# at 23:37 ##设定任务执行时间
at> rm -fr /mnt/* ##任务动作
at> << CTRL +D ##用ctrl+D发起任务
[root@localhost ~]# at now+1min ##延时1分钟
at> rm -fr /mnt/*
at>
at -l ##查看任务列表
at -c 任务号 ##查看任务内容
at -r 任务号 ##取消任务执行
注意:
当任务有输出时,输出会以邮件的型是发送给at任务的发起者
mail -u root ##查看超级用户邮件
1 ##查看第一封邮件
q ##退出
/var/spool/mail/root ##清空邮件

可以看到设置定时任务已经完成


[root@westos_student50 Desktop]# at 02:21
warning: commands will be executed using /bin/sh
at> touch mou
at> <EOT>
job 4 at Tue Aug  9 02:21:00 2022
[root@westos_student50 Desktop]# ls
mou

设置延时一分钟来进行下方的命令,结果显示成功
在这里插入图片描述
用下边的这些命令可以查看任务,以及取消任务

[root@westos_student50 Desktop]# at now+2min 
warning: commands will be executed using /bin/sh
at> touch mou2
at> <EOT>
job 12 at Tue Aug  9 02:43:00 2022
[root@westos_student50 Desktop]# at -l
12	Tue Aug  9 02:43:00 2022 a root
[root@westos_student50 Desktop]# at -c 12
[root@westos_student50 Desktop]# at -r 12
[root@westos_student50 Desktop]# at -l

2.at任务的黑白名单

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

二、定时任务

1.系统定时任务

#/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
4.系统级别的cron(文件方式设定定时任务)
vim /etc/cron.d/file
* * * * * username action
* * * * * root rm -fr /mnt/

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

这时每分钟删除一次目录中的文件
* * * * * root rm -fr /root/Desktop/mou/*
~                                                                                                                                                        
~                                            

在文件中写入定时任务之后,看一下效果

[root@westos_student50 Desktop]# cd /root/Desktop/mou
[root@westos_student50 mou]# touch mou{1..2}
[root@westos_student50 mou]# cd
[root@westos_student50 ~]# cd /root/Desktop/
[root@westos_student50 Desktop]# ls
mou
[root@westos_student50 Desktop]# ls mou/
[root@westos_student50 Desktop]# 

三、系统中临时文件的管理方式

cd /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/*

   99  systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
  100  touch /mnt/westos/file
  101  systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
  102  touch /mnt/westos/file1
  103  systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
  104  touch /mnt/westos/file1
  105  systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*
  106  history
[root@westos_student50 tmpfiles.d]# 

设置了我们所指定的目录下文件的生命周期,在这个周期内不能清楚,但是可以强制删除

/mnt/westos:
total 0
-rw-r--r--. 1 root root 0 Aug  9 03:29 file1

在过了生命周期之后,我们就可以用clean将文件清除

总结

这一节内容相对来说比较少,主要是学习了延时任务和定时任务的设置,以及对文件进行生命周期的设置,虽然内容不多也比较简单,但是对于我们以后的学习工作都很重要

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值