Linux系统中延时任务及定时任务

1.系统延时任务

[root@node4 tftpboot]# at 13:33               ##设定任务执行时间
warning: commands will be executed using /bin/sh
at> touch /mnt/westosfile{1..3}               ##任务动作
at> <EOT>                                     ##用 ctrl+D 发起任务
job 1 at Sun Nov  8 13:33:00 2020

at -r 任务号                                   ##取消任务执行
[root@node4 tftpboot]# at -l                  ##查看任务列表
1	Sun Nov  8 13:33:00 2020 a root
[root@node4 tftpboot]# at -c 1                ##查看任务内容
[root@node4 tftpboot]# dnf search mailx
[root@node4 tftpboot]# dnf install mailx.x86_64 -y
[root@node4 tftpboot]# dnf search postfix
[root@node4 tftpboot]# dnf install postfix.x86_64 -y
[root@node4 tftpboot]# systemctl enable --now postfix
[root@node4 tftpboot]# at now+1min               ##延时 1 分钟
[root@node4 tftpboot]# at 13:42
warning: commands will be executed using /bin/sh
at> date
at> <EOT>
job 4 at Sun Nov  8 13:42:00 2020
[root@node4 tftpboot] mail -u root ##当任务有输出时,输出会以邮件的型是发送给 at 任务的发起者,查看超级用户邮件
& 1              ##查看第一封邮件
& headers 
> 1    root      Sun Nov  8 13:42  14/496   "Output from your job "
& delete 1
& headers     
No applicable messages
& q              ##退出

> /var/spool/mail/root ##清空邮件

注意:当任务有输出时,输出会以邮件的型是发送给 at 任务的发起者
例如使用date命令查看时间时,当通过at指定date任务,该任务的输出将会以邮件的形式发送给该任务的发起者,而不会直接显示,需要查看相关邮件来获得其输出。另外,如果需要查看邮件,则需要先安装mailx和postfix服务,并开启postfix服务。

2.at 任务的黑白名单

[root@node4 tftpboot]# cd 
[root@node4 ~]# su - westos
[westos@node4 ~]$ at now+1min
[root@node4 ~]# vim /etc/at.deny
[root@node4 ~]# su - westos
[westos@node4 ~]$ at now+1min
You do not have permission to use at.
[westos@node4 ~]$ logout
[root@node4 ~]# vim /etc/at.allow
[root@node4 ~]# su - westos
[westos@node4 ~]$ at now+1min
warning: commands will be executed using /bin/sh
at> ^C
[westos@node4 ~]$ logout
[root@node4 etc]# cat /etc/at.deny     
westos
                             ####系统中默认存在,在此文件中出现的用户不能执行 at
[root@node4 etc]# cat /etc/at.allow    
westos
                             ##系统中默认不存在,当文件出现,普通用户不能执行 at 
                             ##只有在白单中的用户可以,并且/etc/at.deny 失效

注意:at.deny文件在系统中是默认存在的,只是初始状态下为空,即没有指定哪些用户不能使用at,如果将整个文件删除,将会导致用户使用at时产生权限错误。

3.系统定时任务

1.crontab 时间表示方式

 minute         0-59
 hour           0-23
 day of month   1-31
 month          1-12 (or names, see below)
 day of week    0-7 (0 or 7 is Sunday, or use names)
* * * * *       ##每分钟
*/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

[root@node4 ~]# crontab -e -u root  ##创建
38 14 * * * touch /mnt/file{1..10}  ##14点38分   分 小时  天 月 周
[root@node4 ~]# crontab -u root -l  ##查看用户级
38 14 * * * touch /mnt/file{1..10}
[root@node4 ~]# cd /var/spool/cron/
[root@node4 cron]# ls
root
[root@node4 cron]# cat root
38 14 * * * touch /mnt/file{1..10}

[root@node4 cron]# crontab -r -u root  ##取消超级用户的任务
[root@node4 cron]# cat root
cat: root: No such file or directory

4.文件方式设定定时任务

vim /etc/cron.d/file    ## cron.d是系统级
* * * * * username action
* * * * * root rm -fr /mnt/*

注意:采用系统级设置时,使用crontab -l无法查看系统级的crontab任务

5.crontab 的黑白名单

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


[root@node4 Desktop]# vim /etc/cron.deny       ##系统中默认存在,在此文件中出现的用户不能执行 crontab
[root@node4 Desktop]# su - westos1
[westos1@node4 ~]$ crontab -e
You (westos1) are not allowed to use this program (crontab)
[westos1@node4 ~]$ logout
[root@node4 Desktop]# su - westos
[westos@node4 ~]$ crontab -e
no crontab for westos - using an empty one
[westos@node4 ~]$ logout
[root@node4 Desktop]# touch /etc/cron.allow    ##只有在名单中的用户可以,并且/etc/at.deny 失效
[root@node4 Desktop]# vim /etc/cron.allow      ##系统中默认不存在,当文件出现,普通用户不能执行 crontab
[root@node4 Desktop]# su - westos1
[westos1@node4 ~]$ crontab -e
no crontab for westos1 - using an empty one
crontab: installing new crontab
[root@localhost cron.d]# su - westos  只有在白名单里的可以
[westos@localhost ~]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information

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

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

[root@node4 Desktop]# cd /usr/lib/tmpfiles.d/ ## 临时文件存储路径
[root@node4 tmpfiles.d]# vim westos.conf      ##编辑文件,创建任务
d /mnt/westos/ 777 root root 8s               ## 有效期 8s
[root@node4 tmpfiles.d]# systemd-tmpfiles  --create /lib/tmpfiles.d/westos.conf  ## 创建临时文件
[root@node4 tmpfiles.d]# touch /mnt/westos/westosfile1
[root@node4 tmpfiles.d]# touch /mnt/westos/westosfile2
[root@node4 tmpfiles.d]# systemd-tmpfiles  --clean /lib/tmpfiles.d/westos.conf  ## 清除临时文件

注意:设置完临时文件的存在周期后在该周期内无法用systemd-tmpfiles --clean命令清楚该文件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值