Unit 14系统的延时,定时任务及临时文件

1.延时任务

就是设置多少时间后执行一个命令,也可以是一个脚本

[root@client138 ~]# date
Sat Jan 19 20:54:21 EST 2019
[root@client138 ~]# at 20:56       ##设定任务的执行时间
at> touch file{1..10}                       ##任务内容

at> <EOT>                                     #ctrl+d 发起任务
job 1 at Sat Jan 19 20:56:00 2019

[root@client138 ~]# at -l               #查看任务列表
2    Sat Jan 19 20:58:00 2019 a root
3    Sat Jan 19 21:00:00 2019 a root

[root@client138 ~]# at -c 3          #查看任务内容
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
XDG_SESSION_ID=2; export XDG_SESSION_ID
HOSTNAME=client138; export HOSTNAME
SHELL=/bin/bash; export SHELL
HISTSIZE=1000; export HISTSIZE
SSH_CLIENT=172.25.254.38\ 59204\ 22; export SSH_CLIENT
SSH_TTY=/dev/pts/0; export SSH_TTY
USER=root; export USER
LS_COLORS=rs=0:di=38\;5\;27:ln=38\;5\;51:mh=44\;38\;5\;15:pi=40\;38\;5\;11:so=38\;5\;13:do=38\;5\;5:bd=48\;5\;232\;38\;5\;11:cd=48\;5\;232\;38\;5\;3:or=48\;5\;232\;38\;5\;9:mi=05\;48\;5\;232\;38\;5\;15:su=48\;5\;196\;38\;5\;15:sg=48\;5\;11\;38\;5\;16:ca=48\;5\;196\;38\;5\;226:tw=48\;5\;10\;38\;5\;16:ow=48\;5\;10\;38\;5\;21:st=48\;5\;21\;38\;5\;15:ex=38\;5\;34:\*.tar=38\;5\;9:\*.

。。。。。。

[root@client138 ~]# at -l
3    Sat Jan 19 21:00:00 2019 a root
[root@client138 ~]# at -r 3     #删除某个待执行的任务:任务号
[root@client138 ~]# at -l

删除后列表就空了

注意:当需要执行的命令有输出时,输出会以邮件的形式发送给at任务的发起者

[root@client138 ~]# at now+1min      ##延时在1min后
at> echo hello rsy!!                                ##发送hello rsy!!
at> <EOT>

job 5 at Sat Jan 19 21:25:00 2019
[root@client138 ~]# at -l
You have mail in /var/spool/mail/root       ##表示接收到邮件
[root@client138 ~]# mail -u root         ##查看root用户的邮件
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/mail/root": 1 message 1 new
>N  1 root                  Sat Jan 19 21:25  14/503   "Output from your job "
& 1                                                  ##选择第一条进行查看
Message  1:
From [email protected]  Sat Jan 19 21:25:01 2019
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Subject: Output from your job        5
To: [email protected]
Date: Sat, 19 Jan 2019 21:25:00 -0500 (EST)
From: [email protected] (root)
Status: R

hello rsy!!

& q              ##输入q可以退出查看邮件
Held 1 message in /var/mail/root

[root@client138 ~]# >/var/spool/mail/root    ##清空邮件内容
[root@client138 ~]# mail -u root
No mail for root

在其他用户中

也可以进行at延时间

[student@client138 ~]$ at now+1min
at> touch file
at> <EOT>

job 8 at Sat Jan 19 21:41:00 2019
[student@client138 ~]$ ls
file

[student@client138 ~]$ at now+1min
at> touch /root/Desktop/file1            ##权限发生错误,不会执行操作
at> <EOT>
job 7 at Sat Jan 19 21:35:00 2019

延时任务的黑白名单

黑名单/etc/at.deny是默认存在的,但里面开始为空,加入黑名单的用户不能使用at指令

[root@client138 ~]# vim /etc/at.deny

编辑内容:student     ##将student加入黑名单
[root@client138 ~]# su - student
Last login: Sat Jan 19 21:32:35 EST 2019 on pts/0
[student@client138 ~]$ at now+1min
You do not have permission to use at.         ##被禁止

白名单在系统中是不存在的,可以自己建立/etc/at.allow文件,在文件中的用户可以使用at指令,其余的普通用户都不可以使用,这时候黑名单就会失效

[root@client138 student]# vim /etc/at.deny     ##首先删除了黑名单中的student用户
[root@client138 student]# vim /etc/at.allow      ##新建白名单,但内容为空,所以student也不可以使用at
[root@client138 student]# su - student
Last login: Sat Jan 19 21:48:21 EST 2019 on pts/0
[student@client138 ~]$ at 21:00            
You do not have permission to use at.

2.定时任务

指定时间进行某项任务

定时配置格式:

* * * * * #每分钟

*/2 * * * *  #每两分钟

*/2 09-17 * * * #早九点到晚五点的每两分钟

*/2 09-17 * 3,5 5 #3月和五月的每周五早九点到晚五点的每两分钟

[root@client138 ~]# crontab -e    ##创建定时任务
no crontab for root - using an empty one
crontab: installing new crontab

*/2 11 * * * touch /mnt/file     ##任务内容:每11点的每两分钟建立/mnt/file文件

[root@client138 ~]# crontab -l -u root   ##查看定时任务内容
*/2 11 * * * touch /mnt/file 
[root@client138 ~]# crontab -r -u root   ##删除定时任务
[root@client138 ~]# crontab -l -u root
no crontab for root

在root中为student用户建立定时任务

[root@client138 ~]# crontab -e -u student ##为student创建定时任务
no crontab for student - using an empty one
crontab: installing new crontab
编辑内容:*/2 09-17 * * * touch /home/student/new

以文件的方式设定定时任务

[root@client138 ~]# cd /etc/cron.d  ##在/etc/cron.d下建立文件
[root@client138 cron.d]# vim cfile
##编辑定时任务内容

当文件内容较少时,可以使用非交互式的方法

echo “*/2 09-17 * 3,5 5”>/etc/cron.d/newfile

3.临时文件的管理

进入/usr/lib/tmpfiles.d/,新建一个file.conf文件

[root@client138 cron.d]# cd /usr/lib/tmpfiles.d/
[root@client138 tmpfiles.d]# ls
abrt.conf                 libselinux.conf  selinux-policy.conf
cups.conf                 lvm2.conf        spice-vdagentd.conf
cups-lp.conf              mdadm.conf       subscription-manager.conf
gvfsd-fuse-tmpfiles.conf  pam.conf         systemd.conf
httpd.conf                ppp.conf         systemd-nologin.conf
initscripts.conf          rpm.conf         tmp.conf
iscsi.conf                samba.conf       x11.conf
legacy.conf               saslauthd.conf
[root@client138 tmpfiles.d]# vim redhat.conf     ##新建文件
[root@client138 tmpfiles.d]# cat redhat.conf
d /mnt/westos 777 root root 5s          ##临时管理内容
d /mnt/westos 777 root root 5s
目录 指定目录 权限 拥有者 所属组 时间
[root@client138 tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*   ##读取所有设定文件

[root@client138 tmpfiles.d]# cd /mnt/westos
[root@client138 westos]# touch file{1..5}
[root@client138 westos]# ls
file1  file2  file3  file4  file5         ##创立了5个文件

[root@client138 westos]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*         #按规则删除


[root@client138 westos]# ls          ##file1~5存在超过5秒被删除

猜你喜欢

转载自blog.csdn.net/weixin_41884844/article/details/86559526