系统延时任务及定时任务

1.设定延时任务

[[email protected] ~]# at 23:37 ##设定任务执行时间 at> rm -fr /mnt/* ##任务动作 at>
<<CTRL+D ##用ctrl+d发起任务

[[email protected] ~]# at now+1min ##延时一分钟 at> rm -fr /mnt/* ##任务动作 at>
<<CTRL+D ##用ctrl+d发起任务

at -l ##查看任务列表 at -c 任务号 ##查看任务内容 at -r 任务号 ##取消任务执行
注意:当有任务输出时,输出会以邮件的形式发送给at任务的发起者 mail -u root ##查看超级用户邮件 l ##查看第一封邮件
q ##退出

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

举例:
(1)设定延时任务并取消
<1>首先开启一个shell监控/mnt下文件及目录的变化

[[email protected] ~]# watch -n 1 ls /mnt

系统延时任务及定时任务
系统延时任务及定时任务
<2>在/mnt下创建10个文件

[[email protected] ~]# at 15:03
at> touch /mnt/file{1..10}
at> <EOT>
job 1 at Tue Jan 29 15:03:00 2019

系统延时任务及定时任务
<3>查看此延时任务的任务号

[[email protected] ~]# at -l
1	Tue Jan 29 15:03:00 2019 a root

系统延时任务及定时任务
<4>查看想要查看的延时任务的内容

[[email protected] ~]# at -c 1
……
${SHELL:-/bin/sh} << 'marcinDELIMITER13b4a5b8'
touch /mnt/file{1..10}

marcinDELIMITER13b4a5b8

系统延时任务及定时任务
<5>取消此延时任务。再次查看时会发现此延时任务的任务号已经消失

[[email protected] ~]# at -l
1	Tue Jan 29 15:03:00 2019 a root
[[email protected] ~]# at -r 1
[[email protected] ~]# at -l

系统延时任务及定时任务
(2)成功完成延时任务
<1>首先开启一个shell监控/mnt下文件及目录的变化

[[email protected] ~]# watch -n 1 ls /mnt

系统延时任务及定时任务
系统延时任务及定时任务
<2>在/mnt下创建10个文件

[[email protected] ~]# at 15:11
at> touch /mnt/file{1..10}
at> <EOT>
job 2 at Tue Jan 29 15:11:00 2019

系统延时任务及定时任务
<3>到达指定的任务时间后,会发现/mnt下成功生成了10个文件
系统延时任务及定时任务
(3)查看超级用户邮件
<1>首先清空邮件

[[email protected] ~]# >/var/spool/mail/root

系统延时任务及定时任务
<2>设定在1min后完成延时任务

[[email protected] ~]# at now+1min
at> echo hahahaha
at> <EOT>
job 4 at Tue Jan 29 15:21:00 2019

系统延时任务及定时任务
<3>如果没有到达指定的时间会出现以下报错(因为是root用户发起的,所以用户是root)

[[email protected] ~]# mail -u root
No mail for root

系统延时任务及定时任务
<4>查看邮件,成功查看到任务输出的内容

[[email protected] ~]# mail -u root
& 1	##查看第一封邮件
& q	##退出

系统延时任务及定时任务

2.at任务的黑白名单

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

(1)前提
<1>首先创建3个用户并查看其是否存在

[[email protected] ~]# id bai
uid=1001(bai) gid=1001(bai) groups=1001(bai)
[[email protected] ~]# id gege
uid=1002(gege) gid=1002(gege) groups=1002(gege)
[[email protected] ~]# id westos
uid=1003(westos) gid=1003(westos) groups=1003(westos)

系统延时任务及定时任务
<2>切换到用户名称为bai的用户下,尝试设定延时任务,发现可以设定

[[email protected] ~]# su - bai
[[email protected] ~]$ at now+1min
at> <EOT>
job 5 at Tue Jan 29 15:27:00 2019
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
<3>切换到用户名称为gege的用户下,尝试设定延时任务,发现可以设定

[[email protected] ~]# su - gege
Last login: Tue Jan 29 15:28:08 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
at> <EOT>
job 7 at Tue Jan 29 15:29:00 2019
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
(2)将用户bai添加至黑名单中
<1>进入到黑名单中并将用户bai添加到黑名单中

[[email protected] ~]# vim /etc/at.deny

系统延时任务及定时任务
添加的内容如下:

bai

系统延时任务及定时任务
<2>再次切换到用户名称为bai的用户下,尝试设定延时任务,发现已经被禁止

[[email protected] ~]# su - bai
Last login: Tue Jan 29 15:26:09 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
You do not have permission to use at.
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
<3>切换到用户名称为gege的用户下,尝试设定延时任务,发现可以设定

[[email protected] ~]# su - gege
Last login: Tue Jan 29 15:28:36 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
at> <EOT>
job 8 at Tue Jan 29 15:31:00 2019
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
(3)配置白名单
当白名单出现之后,黑名单就失效了
<1>进入白名单,发现没有此文件,创建一个白名单文件(在文件内什么都不写)

[[email protected] ~]# cd /etc/at.allow
bash: cd: /etc/at.allow: No such file or directory
[[email protected] ~]# touch /etc/at.allow
[[email protected] ~]# vim /etc/at.allow

<2>切换到用户名称为bai的用户下,尝试设定延时任务,发现已经被禁止

[[email protected] ~]# su - bai
Last login: Tue Jan 29 15:29:39 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
You do not have permission to use at.
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
<3>切换到用户名称为gege的用户下,尝试设定延时任务,发现已经被禁止

[[email protected] ~]# su - gege
Last login: Tue Jan 29 15:47:59 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
You do not have permission to use at.
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
<4>在白名单配置文件中添加用户bai(bai并没有从黑名单中删除)

[[email protected] ~]# vim /etc/at.allow

系统延时任务及定时任务
添加的内容

bai

系统延时任务及定时任务
<5>再次切换到用户名称为bai的用户下,尝试设定延时任务,发现可以设定

[[email protected] ~]# su - bai
Last login: Tue Jan 29 15:46:20 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
at> <EOT>
job 9 at Tue Jan 29 15:51:00 2019
[[email protected] ~]$ exit
logout

系统延时任务及定时任务
<6>再次切换到用户名称为gege的用户下,尝试设定延时任务,发现依旧不可以设定

[[email protected] ~]# su - gege
Last login: Tue Jan 29 15:48:08 CST 2019 on pts/1
[[email protected] ~]$ at now+1min
You do not have permission to use at.
[[email protected] ~]$ exit
logout

系统延时任务及定时任务

3.系统定时任务

1.crontab时间表示方式
* * * * *	##每分钟
*/2 * * * *	##每两分钟
*/2 09-17 * * *	##早上7点到晚上5点每两分钟
*/2 */2 * * *	##每隔2小时每2分钟
*/2 09-17 3,5 1 5	##1月的3号和5号以及每周的周五
*/2 09-17 * * 5		##每周的周五的早上9点到晚上5点
2.系统控制crontab的服务
crond.service	##当程序开启时定时任务生效
3.crontab
crontab -e -u
crontab -l -u
crontab -r -u
4.文件方式设定定时任务
vim /etc/cron.d/file
* * * * *  username action
* * * * *  root rm -rf /mnt/*
5.crontab的黑白名单
/etc/cron.deny	##系统中默认存在,在此文件中出现的用户不能执行crontab
/etc/cron.allow	##系统中默认不存在,当文件出现时,普通用户不能执行crontab,只有在名单中的用户可以,并且/etc/cron.deny失效
注意:这两个名单都不会影响/etc/cron.d目录中定时任务的发起与执行

举例:
(1)定时删除/mnt目录下的所有文件
<1>首先监控/mnt下目录及文件的变化

[[email protected] ~]# watch -n 1 ls /mnt

系统延时任务及定时任务
<2>首先设定定时任务

[[email protected] ~]# crontab -e -u root
no crontab for root - using an empty one
crontab: installing new crontab

系统延时任务及定时任务
<3>查看定时任务的内容

[[email protected] ~]# crontab -l -u root
09 18 * * * rm -rf /mnt/*

系统延时任务及定时任务
<4>时间到达时,/mnt目录下文件的变化,发现/mnt下的文件都被删除
系统延时任务及定时任务
(2)利用文件方式设定定时任务
<1>首先监控/mnt下目录及文件的变化

[[email protected] ~]# watch -n 1 ls /mnt

系统延时任务及定时任务
<2>进入到配置定时任务的目录中

[[email protected] ~]# cd /etc/cron.d
[[email protected] cron.d]# ls
0hourly  raid-check  sysstat

系统延时任务及定时任务
<3>进入/etc/cron.d目录下的任意一个文件进行内容的编写

[[email protected] cron.d]# vim westos

系统延时任务及定时任务
在文件中写入的内容如下:

* * * * * root rm -rf /mnt/file{1..3}

系统延时任务及定时任务
<4>等到整分时间过去后,会发现/mnt目录下的file1,file2,file3被删除
系统延时任务及定时任务
(3)设定定时任务的黑名单
<1>进入到定时任务的黑名单配置中并将用户westos写入其中

[[email protected] cron.d]# vim /etc/cron.deny

系统延时任务及定时任务
在配置文件中的配置如下:
系统延时任务及定时任务
<2>切换到westos用户下并试图设定定时任务,会发现设定失败

[[email protected] cron.d]# su - westos
Last login: Tue Jan 29 18:15:47 CST 2019 on pts/0
[[email protected] ~]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information

系统延时任务及定时任务
(4)设定定时任务的白名单
<1>首先查看定时任务的配置文件中是否有白名单,默认是不存在的

[[email protected] ~]$ ls -ld /etc/cron.deny
-rw-------. 1 root root 7 Feb  5 15:13 /etc/cron.deny
[[email protected] ~]$ ls -ld /etc/cron.allow
ls: cannot access /etc/cron.allow: No such file or directory

系统延时任务及定时任务
<2>创建定时任务的白名单

[[email protected] ~]# touch /etc/cron.allow

系统延时任务及定时任务
<3>再次切换到用户westos下试图设定定时任务,会发现依旧无法设定,因为白名单只是创建了,并没有进行配置

[[email protected] ~]# su - westos
Last login: Tue Feb  5 15:13:44 CST 2019 on pts/1
[[email protected] ~]$ crontab -e
You (westos) are not allowed to use this program (crontab)
See crontab(1) for more information

系统延时任务及定时任务
<4>对定时任务的白名单进行配置,设定westos用户可以设定定时任务(黑名单中依旧存在westos)

[[email protected] ~]# vim /etc/cron.allow

系统延时任务及定时任务
配置的内容如下:
系统延时任务及定时任务
注意:配置时一定要注意是在超级用户下才可以进行配置
<5>切换到用户westos下试图进行设定定时任务,发现可以成功设定

[[email protected] ~]# su - westos
Last login: Tue Feb  5 15:22:41 CST 2019 on pts/1
[[email protected] ~]$ crontab -e
no crontab for westos - using an empty one
crontab: installing new crontab

系统延时任务及定时任务
总结:白名单的优先级大于黑名单,既如果白名单存在,那么只有在白名单内的用户可以设定定时任务

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

cd /usr/lib/tmpfiles.d/ vim westos.conf d /mnt/westos 777 root root
8s(类型 文件名 权限 所有人 所有组 保留时间) systemd-tmpfiles --create
/usr/lib/tmpfiles.d/*

<1>首先查看目录/tmp/的权限及属性

[[email protected] ~]# ls -ld /tmp/
drwxrwxrwt. 58 root root 4096 Feb  5 15:44 /tmp/

系统延时任务及定时任务
<2>进入临时文件的配置中并进行查看

[[email protected] ~]# cd /usr/lib/tmpfiles.d/
[[email protected] tmpfiles.d]# ls

系统延时任务及定时任务
<3>进入/usr/lib/tmpfiles.d/tmp.conf中进行查看,会发现其内有对文件的管理

[[email protected] tmpfiles.d]# vim tmp.conf

系统延时任务及定时任务
<4>利用watch命令对/mnt目录下的文件及目录进行查看

[[email protected] ~]# watch -n 1 ls -lR /mnt

系统延时任务及定时任务
<5>新建一个临时文件对其进行文件管理的编写

[[email protected] tmpfiles.d]# vim westos.conf
[[email protected] tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/westos.conf

系统延时任务及定时任务
编写的内容如下:

d /mnt/westos 1777 root root 10s

<6>在/mnt/westos下创建两个文件file1,file2,并尝试在10s清除两个文件,发现无法清除(因为之前设置的临时文件的保留时间最少是10s)

[[email protected] tmpfiles.d]# touch /mnt/westos/file1[[email protected] tmpfiles.d]# touch /mnt/westos/file2
[[email protected] tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/westos.conf

系统延时任务及定时任务
<7>在保留时间后清除便可以清除临时文件下的所有文件

[[email protected] tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/westos.conf

系统延时任务及定时任务
<8>保留时间超出10s的,如果没有超出保留时间,也只能清除一部分,假设创建file3超出了10s,而file4未超出10s,对临时文件进行清除

[[email protected] tmpfiles.d]# touch /mnt/westos/file3    ##保留时间超出了10s
[[email protected] tmpfiles.d]# touch /mnt/westos/file4   ##保留时间未超出10s
[[email protected] tmpfiles.d]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/westos.conf

系统延时任务及定时任务