系统延时任务及定时任务

本文介绍了Linux系统中的延迟任务与定时任务管理,包括使用`at`和`cron`命令设置任务,以及权限控制文件`at.deny`和`cron.allow`的使用。同时,讲解了系统级别的定时任务配置以及`tmpfiles.d`目录下管理临时文件的方法,展示了如何创建、删除和清理临时文件。

1.系统延时任务

[root@westoshost214 Desktop]# watch -n 1 ls -l /mnt/  监控

测试

[root@westoshost214 Desktop]# at 13:45   设定任务执行时间  at now+1min延时1分钟

warning: commands will be executed using /bin/sh

at> touch /mnt/westos{1..3}任务动作

at> <EOT> 按ctrl + D 发起执行

job 2 at Fri Oct 22 13:45:00 2021

[root@westoshost214 Desktop]# at -l  查看任务列表

2 Fri Oct 22 13:45:00 2021 a root

[root@westoshost214 Desktop]# at -r 2 取消任务执行

[root@westoshost214 Desktop]# at -c 2 查看任务内容

2.at任务的黑白名单

[root@westoshost214 Desktop]# ll /etc/at.deny 默认存在 黑名单 文件中出现的用户不能执行at命令

[root@westoshost214 Desktop]# vim /etc/at.deny  文件里边有westos .lee 用户

[root@westoshost214 Desktop]# su - westos

Last login: Fri Mar 26 14:04:37 CST 2021 on tty2

[westos@westoshost214 ~]$ at now+1min

You do not have permission to use at.

[lee@westoshost214 ~]$ at 14:15

You do not have permission to use at.

[root@westoshost214 Desktop]# ll /etc/at.allow   默认不存在  需要建立 当文件出现的时候,只有root用户可以执行 at命令 ;只有在白名单里边出现的普通用户可以执行at命令 并且文件出现后/etc/at.deny失效  

ls: cannot access '/etc/at.allow': No such file or directory

[root@westoshost214 ~]# touch /etc/at.allow

[root@westoshost214 ~]# vim /etc/at.allow   文件里边没有westos用户

[root@westoshost214 ~]# at now+1week

warning: commands will be executed using /bin/sh

at> ^C[root@westoshost214 ~]# exit

[westos@westoshost214 ~]$ at 15:00

You do not have permission to use at.

 3.系统定时任务 

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

系统控制crontab的服务  crond.service

 systemctl status crond.service  查看程序是否开启

##当程序开启时定时任务生效

 crontab -u root -e 进入超级用户编辑模式

[root@westoshost100 ~]# crontab -u root -l   显示超级用户定时操作的内容

* * * * * rm -fr /mnt/*  每1分钟删除/mnt/的所有文件

crontab -u root -r  删除定时任务的内容   ( 如果不指定用户,则表示显示当前用户) 

 crontab 时间表示方式

*/2 * * * * 每2分钟

0 */2 * * * 每2小时

0 */2 1 * *  每月1号的每2小时整点(0)

0 */2 1,5 * *  每月1号和5号的每2小时

0 */2 1,5 3-5 *  3月到5月的1号和5号每两小时

0 */2 1,5 3-5 5   3月到5月的1号和5号或者周5的每两小时

*分钟 *小时  *日期 *月份 *星期 要运行的命令(如果有多个命令用&&隔开) /指定时间的频率  -表示一个范围 ,某一个值

4.系统级别的cron(文件方式设定定时任务)

[root@westoshost100 Desktop]# cd /etc/cron.d/

[root@westoshost100 cron.d]# ls

0hourly  raid-check

[root@westoshost100 cron.d]# vim westos

[root@westoshost100 cron.d]# cat westos   

* * * * * root rm  -fr /mnt/*

[root@westoshost100 cron.d]# rm -fr westos  系统级的定时任务 不能用crontab命令的方式删除

[root@westoshost100 cron.d]# ls

0hourly  raid-check

 5.crontab的黑白名单

[root@westoshost214 cron.d]# vim /etc/cron.deny   crontab的黑名单 系统默认存在,在此文件中出现的用户不能执行crontab命令

[roots@westoshost214 ~]$ cat /etc/cron.deny

lee

[root@westoshost214 cron.d]# su - lee  

[lee@westoshost214 ~]$ crontab -e  不能执行

You (lee) are not allowed to use this program (crontab)

See crontab(1) for more information

[westos@westoshost214 ~]$ crontab -e  可以执行

crontab: installing new crontab

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

[root@westoshost214 cron.d]# cat /etc/cron.allow

westos

[root@westoshost214 cron.d]# crontab -e  可以执行

[root@westoshost214 cron.d]# su - lee    不能执行

Last login: Fri Oct 22 16:12:14 CST 2021 on pts/1

[lee@westoshost214 ~]$ crontab -e

You (lee) are not allowed to use this program (crontab)

See crontab(1) for more information

[westos@westoshost214 ~]$ crontab -e  可以执行

crontab: installing new crontab

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

监控

[root@westoshost100 Desktop]# watch -n 1 ls -lR /mnt/

测试:

[root@westoshost100 Desktop]# cd /lib/tmpfiles.d/

[root@westoshost100 tmpfiles.d]# vim  westos.conf

[root@westoshost100 tmpfiles.d]# cat /lib/tmpfiles.d/westos.conf

d /mnt/westos 1777 root root 5s

[root@westoshost100 ~]# systemd-tmpfiles --create /lib/tmpfiles.d/westos.conf

[root@westoshost100 ~]# touch /mnt/westos/file

[root@westoshost100 ~]# systemd-tmpfiles --clean /lib/tmpfiles.d/westos.con

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值