系统定时任务及延时任务

本文介绍了Linux系统中使用at和cron进行任务调度的方法,包括任务创建、查看、取消以及权限管理。同时,讲解了系统临时文件的管理,如通过westos.conf配置文件进行清理。内容涵盖时间表达式、用户权限控制以及系统服务的启用。
摘要由CSDN通过智能技术生成

一、系统延时任务

rm -fr /mnt/*
touch /mnt/file{1..10}

at now+1min   ##1分钟后执行任务
warning: commands will be executed using /bin/sh
at> rm -fr /mnt/*
at> <EOT>           ##ctrl+d发起任务
job 1 at Fri Oct 22 13:43:00 2021

at -l     ##查看任务列表
2	Fri Oct 22 13:45:00 2021 a root
at -c 1    ##查看任务1
at 13:48  ##在13:48分开始任务
warning: commands will be executed using /bin/sh
at> touch /mnt/file
at> <EOT>
job 3 at Fri Oct 22 13:48:00 2021
at -r 6    ##取消任务

 

 二、at任务的黑白名单

/etc/at.deny     ##系统中默认存在,在此文件中出现的用户不能执行at
/etc/at.allow    ##系统中默认不存在,当文件出现,普通用户不能执行at
                 ##只有在名单中的用户可以,并且/etc/at.deny失效
[root@westoslinux ~]# vim /etc/at.deny     ##将用户xmm写入黑名单
[root@westoslinux ~]# su - xmm
[xmm@westoslinux ~]$ at now+1min
You do not have permission to use at.    ##用户xmm不可执行at命令
[xmm@westoslinux ~]$ exit
logout
[root@westoslinux ~]# touch /etc/at.allow   ##建立白名单,一旦白名单出现黑名单便失效,只有白名单内用户和root可执行
[root@westoslinux ~]# su - westos   ##westos用户不可执行
Last login: Mon Oct 18 17:12:46 CST 2021 on pts/0
[westos@westoslinux ~]$ at now+1min
You do not have permission to use at.
[westos@westoslinux ~]$ exit
logout
[root@westoslinux ~]# vim /etc/at.allow   ##将用户xmm添进白名单
[root@westoslinux ~]# su - xmm
Last login: Mon Oct 25 01:34:09 CST 2021 on pts/2
[xmm@westoslinux ~]$ at now+1min    ##xmm可执行at任务
warning: commands will be executed using /bin/sh
at> ^C

三、系统定时任务

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

1.crontab 时间表示方式

* * * * *    ##每分钟
*/2 * * * *  ##每两分钟
*/2 09-17 * * *   ##早9-晚5每两分钟
*/2 */2 * * *    ##每隔2小时每两分钟
*/2 09-17 3,5 1 5  ##1月的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/*

 

5.crontab的黑白名单

/etc/cron.deny   ##系统中默认存在,在此文件中出现的用户不能执行crontab

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

[root@westoshost111 cron.d]# vim /etc/cron.deny   ##设置黑名单
[root@westoshost111 cron.d]# su - lee
Last login: Fri Oct 22 14:16:18 CST 2021 on pts/2
[lee@westoshost111 ~]$ crontab -e
You (lee) are not allowed to use this program (crontab)
See crontab(1) for more information
[lee@westoshost111 ~]$ exit
logout
[root@westoshost111 cron.d]# touch /etc/cron.allow ##设置白名单
[root@westoshost111 cron.d]# vim /etc/cron.allow
[root@westoshost111 cron.d]# su - lee
Last login: Fri Oct 22 15:24:05 CST 2021 on pts/2
[lee@westoshost111 ~]$ crontab -e
no crontab for lee - using an empty one
crontab: no changes made to crontab
[lee@westoshost111 ~]$ exit
logout
[root@westoshost111 cron.d]# su - westos
Last login: Fri Oct 22 14:15:35 CST 2021 on pts/2
[westos@westoshost111 ~]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information

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

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

删除mnt下5s之前建立的所有文件

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值