定时任务和延时任务

1.系统延时任务
[root@server-sshd Desktop]# at 7:16  #设定任务执行时间
at> rm -rf /mnt/*   #任务动作
at> <EOT>           #用ctrl+d发起任务

注意:为了看到效果,这里使用watch -n 2 ls -lR /mnt监测/mnt目录

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务


效果图:

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_02

[root@server-sshd Desktop]# at now+1min  #延时1分钟
at> rm -rf /mnt/*
at> <EOT>

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_03

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

注意:查看的任务不要是已经执行过了的,否则无法查询到

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务_04

[root@server-sshd Desktop]# at -c 7  #查看任务内容,7是任务号

centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_05


centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_06

[root@server-sshd Desktop]# at -r 任务号  #取消任务执行

注意:
当任务有输出时,输出会以邮件的形式发给at任务的发起者

[root@server-sshd Desktop]#mail -u root   #查看超级用户邮件
at>1     #查看第一封邮件
at>q     #退出

为了看到效果,可以先执行以下命令

[root@server-sshd Desktop]# > /var/spool/mail/root  #清空邮件

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务_07

2.at任务的黑白名单

文件

含义

/etc/at.deny

黑名单,系统中默认存在。在此文件中出现的用户不能执行at

/etc/at.allow

白名单,系统中默认不存在,当文件出现。普通用户不能执行at

注意:如果出现白名单,只有名单中的用户可以,并且/etc/at.deny黑名单失效

首先切换到普通用户student发现可以执行延时操作

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_08

切换回超级用户root在黑名单中添加student用户

[root@server-sshd Desktop]# vim /etc/at.deny

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务_09


再次切换到student用户会发现无法执行延时操作

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务_10


如果用超级用户再建立白名单添加student,则可以看到student恢复延时功能,黑名单此时失效

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务_11


centos 定时任务每秒执行一个任务 linux定时任务秒_vim_12

3.系统定时任务

1).crontab 时间表示方式

命令

含义

* * * * *

#每分钟

*/2 * * * *

#每两分钟

*/2 09-17 * * *

#早上7点-晚上5点的每两分钟

*/2 */2 * * *

#每隔2小时每2分钟(这两个两个小时中每两分钟)

*/2 09-17 3,5 1 5

#1月的3号和5号早9-晚5的每两分钟,以及每周五的早9-晚5的每两分钟

*/2 09-17 * * 5

#每周周五早9点-晚5点的每两分钟

2).系统控制crontab的服务

查看crontab.service 服务的状态,保证服务必须是开启

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

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_13


3).crontab

命令

含义

crontab -e -u

#创立一个新文件操作者是root用户,对某个目录进行定时操作

crontab -l -u

#查看root用户的定时操作

crontab -r -u

#删除root用户的定时操作

[root@server-sshd cron.d]#  crontab -e -u  root   #创立一个新文件操作者是root用户,对某个目录进行定时操作,此处表示每分钟删除一次/mnt目录下的文件

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_14

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_15

[root@server-sshd cron.d]# crontab -l -u root  #查看root用户的定时操作

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_16


[root@server-sshd cron.d]# crontab -r -u root #删除root用户的定时操作

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_17


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

vim /etc/cron.d/file

内容: * * * * * username action

例子: * * * * * root rm -rf /mnt/* #表示root用户在mnt目录下每分钟执行一次清理

文件

含义

/etc/cron.deny

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

/etc/cron.allow

#系统中默认不存在,当文件出现,普通用户不能执行crontab,只有在名单中的用户可以,并且/etc/cron.deny 名单失效

vim /etc/cron.deny #编辑添加用户redhat

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_18


centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_19


centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_20


发现redhat已经无法使用crontab命令,切换到student却可以

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_21


建立/etc/cron.allow文件

centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_22


切换到student用户,发现也不能使用crontab命令

centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_23


centos 定时任务每秒执行一个任务 linux定时任务秒_vim_24


当/etc/cron.allow 文件出现,/etc/cron.deny 名单失效

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

[root@server-sshd Desktop]#  cd /usr/lib/tmpfiles.d/  #打开进入/usr/lib/tmpfiles.d/目录下,并查看内容

centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_25


[root@server-sshd tempfiles.d]# vim westos.conf #新建编辑westos文件

centos 定时任务每秒执行一个任务 linux定时任务秒_centos 定时任务每秒执行一个任务_26


添加内容: d /mnt/westos 1777 root root 10s

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_27


查看内容:

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_28


监控mnt目录并做以下操作:[root@server-sshd tempfiles.d]# systemd-tempfiles --create /usr/lib/tmpfiles.d/westos.conf

centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_29

[root@server-sshd tempfiles.d]# systemd-tempfiles --clean /usr/lib/tmpfiles.d/westos.conf

超过10s才能删除否则不能删除

centos 定时任务每秒执行一个任务 linux定时任务秒_Desktop_30


以下是时间未到,所以无法清除

centos 定时任务每秒执行一个任务 linux定时任务秒_root用户_31


centos 定时任务每秒执行一个任务 linux定时任务秒_vim_32


时间到了再次清除就可以清除掉了

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_33

centos 定时任务每秒执行一个任务 linux定时任务秒_vim_34