ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-10-25 18:58:54  阅读:205  来源: 互联网

标签:westoshost139 lee ## westos crontab 任务 延时 Linux root


一.系统延时任务

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之前建立的所有文件 :

标签:westoshost139,lee,##,westos,crontab,任务,延时,Linux,root
来源: https://blog.csdn.net/weixin_44743132/article/details/120956048

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有