Linux第二本书第四章 系统定时任务与延时任务

2021/10/26 7:11:45

本文主要是介绍Linux第二本书第四章 系统定时任务与延时任务,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一.系统延时任务

rm -fr /mnt/*
touch /mnt/file{1..7}
at now+1min   ##1分钟后执行任务
warning: commands will be executed using /bin/sh
at> rm -fr /mnt/*
at> <EOT>           ##ctrl+d发起任务

at -l     ##查看任务列表
at -c 任务号   ##查看任务
at -r 任务号    ##取消任务

at 17:32  ##在17:32分开始任务
at> rm -fr /mnt/*
at> <EOT>

at now+1day  ##延时一天

 

二.at任务的黑白名单

/etc/at.deny   ##系统中默认存在,在此文件中出现的用户不能执行at
/etc/at.allow   ##系统中默认不存在,当文件出现,普通用户不能执行at
                 ##只有在名单中的用户可以,并且/etc/at.deny失效
[root@westoshost139 ~]# vim /etc/at.deny  ##将lee用户加入黑名单
[root@westoshost139 ~]# su - lee
Last login: Sat Oct 23 20:36:02 CST 2021 from 172.25.254.239 on pts/1
[lee@westoshost139 ~]$ at now+2min
You do not have permission to use at.   ##用户lee不可使用at命令
[lee@westoshost139 ~]$ exit
logout
[root@westoshost139 ~]# touch /etc/at.allow  ##建立白名单,白名单一旦出现,黑名单便会失效,只有白名单内用户和root可执行
[root@westoshost139 ~]# su - westos
Last login: Sat Oct 23 20:36:27 CST 2021 from 172.25.254.239 on pts/1
[westos@westoshost139 ~]$ at now+2min     ##westos用户不可执行
You do not have permission to use at.
[westos@westoshost139 ~]$ exit
logout
[root@westoshost139 ~]# vim /etc/at.allow   ##将westos用户加入白名单
[root@westoshost139 ~]# cat /etc/at.allow
westos
[root@westoshost139 ~]# su - westos
Last login: Mon Oct 25 17:44:30 CST 2021 on pts/0
[westos@westoshost139 ~]$ at now+2min            ##用户westos可以执行at命令
warning: commands will be executed using /bin/sh
at> ^C[westos@westoshost139 ~]$ ^C
[westos@westoshost139 ~]$ 

 

三.系统定时任务

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

1.crontab 时间表示方式

* * * * *                    ##每分钟执行一次
*/2 * * * *                  ##每两分钟
*/2 09-17 * * *              ##每天的早9-晚5每两分钟
*/2 */2 * * *                ##每隔两小时两分钟执行一次
*/2 09-17 3,5 1 5            ##一月的周五和一月3号和5号,每天的早9-晚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/*
* * * * *   westos  touch /mnt/gyy{1..3}    ##需要给/mnt/设置权限

5.crontab的黑白名单

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

[root@westoshost110 cron.d]# vim /etc/cron.deny    ##将用户lee加入黑名单
[root@westoshost110 cron.d]# su - lee
Last login: Fri Oct 22 15:22:57 CST 2021 on pts/1
[lee@westoshost110 ~]$ crontab -e           ##用户lee不可执行crontab -e 命令
You (lee) are not allowed to use this program (crontab)
See crontab(1) for more information
[lee@westoshost110 ~]$ exit
logout
[root@westoshost110 cron.d]# crontab -u lee -e    ##超级用户可以指定用户lee去执行(即使其在黑名单中,也需要执行命令)
no crontab for lee - using an empty one
crontab: installing new crontab
[root@westoshost110 cron.d]# touch /mnt/gyy          
[root@westoshost110 cron.d]# touch /etc/cron.allow    ##建立白名单,系统中默认不存在,当文件出现,普通用户不能执行crontab命令
[root@westoshost110 cron.d]# su - lee              
Last login: Fri Oct 22 15:24:04 CST 2021 on pts/1
[lee@westoshost110 ~]$ crontab -e
You (lee) are not allowed to use this program (crontab)  ##用户lee不能执行
See crontab(1) for more information
[lee@westoshost110 ~]$ exit
logout
[root@westoshost110 cron.d]# vim /etc/cron.allow        ##将lee加入白名单中
[root@westoshost110 cron.d]# su - lee
Last login: Fri Oct 22 15:30:21 CST 2021 on pts/1            
[lee@westoshost110 ~]$ crontab -e                        ##用户lee可以执行     
crontab: installing new crontab
[lee@westoshost110 ~]$ exit
logout
[root@westoshost110 cron.d]# su - westos
Last login: Fri Oct 22 15:29:51 CST 2021 on pts/1
[westos@westoshost110 ~]$ crontab -e                   ##用户westos没有在白名单,不可执行
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information
[westos@westoshost110 ~]$ exit
logout
[root@westoshost110 cron.d]# 

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

[root@westoshost139 tmpfiles.d]# cd 
[root@westoshost139 ~]# vim /etc/cron.d/westos  ##将之前建立的相关系统级别的定时任务清除
[root@westoshost139 ~]# cd /lib/tmpfiles.d/
[root@westoshost139 tmpfiles.d]# vim /lib/tmpfiles.d/westos.conf
[root@westoshost139 tmpfiles.d]# cat /lib/tmpfiles.d/westos.conf
d /mnt/westos 777 root root 5s
[root@westoshost139 tmpfiles.d]# systemd-tmpfiles --create /lib/tmpfiles.d/westos.conf
[root@westoshost139 tmpfiles.d]# touch /mnt/westos/gyy1
[root@westoshost139 tmpfiles.d]# systemd-tmpfiles --clean /lib/tmpfiles.d/westos.conf
[root@westoshost139 tmpfiles.d]# 

删除/mnt/westos/目录下5s之前建立的所有文件 :



这篇关于Linux第二本书第四章 系统定时任务与延时任务的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程