当前位置:网站首页>(十四)时间延时任务及定时任务

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

2022-08-10 02:17:00 moumumu

前言

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

一、延时任务

1.系统延时任务

[[email protected] ~]# at 23:37 ##设定任务执行时间
at> rm -fr /mnt/* ##任务动作
at> << CTRL +D ##用ctrl+D发起任务
[[email protected] ~]# 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 ##清空邮件

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


[[email protected]_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
[[email protected]_student50 Desktop]# ls
mou

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

[[email protected]_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
[[email protected]_student50 Desktop]# at -l
12	Tue Aug  9 02:43:00 2022 a root
[[email protected]_student50 Desktop]# at -c 12
[[email protected]_student50 Desktop]# at -r 12
[[email protected]_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/*
~                                                                                                                                                        
~                                            

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

[[email protected]_student50 Desktop]# cd /root/Desktop/mou
[[email protected]_student50 mou]# touch mou{1..2}
[[email protected]_student50 mou]# cd
[[email protected]_student50 ~]# cd /root/Desktop/
[[email protected]_student50 Desktop]# ls
mou
[[email protected]_student50 Desktop]# ls mou/
[[email protected]_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
[[email protected]_student50 tmpfiles.d]# 

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

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

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

总结

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

原网站

版权声明
本文为[moumumu]所创,转载请带上原文链接,感谢
https://blog.csdn.net/moumumu/article/details/126239622