linux系统延时、定时任务的设定

系统延时任务

延时任务:在某段时间之后或者在某个固定的时间执行设定的任务,该任务只执行一次。

1 at

at 固定的时间

[root@server ~]# date                    查看时间节点
Sat May 11 19:03:53 PDT 2019
[root@server ~]# at 19:05                        设置固定时间任务
at> touch Desktop/file                           设置任务
at> <EOT>                                   crtl + d  发起定时任务设置
job 3 at Sat May 11 19:05:00 2019
[root@server ~]# date    
Sat May 11 19:09:24 PDT 2019
[root@server ~]# cd Desktop/
[root@server Desktop]# ls
Discuz_X3.2_SC_UTF8.zip  file                   查看已经设置好的
[root@server Desktop]# 

at now +时间
[root@foundation47 ~]# at now+1min         #设定任务执行时间 1分钟之后
at> rm -fr Desktop/file                    #设定任务的操作命令
at> <EOT>                                  #ctrl+d 发起延时任务
job 3 at Sat Apr 13 13:36:00 2019
at -l #查看延时任务列表
[root@server Desktop]# date
Sat May 11 19:32:29 PDT 2019
[root@server Desktop]# at 19:35
at> touch Desktop/file
at> <EOT>
job 11 at Sat May 11 19:35:00 2019
[root@server Desktop]# at -l                
11 Sat May 11 19:35:00 2019 a root            此为任务列表
at -c 任务号 # 查看延时任务内容
[root@server Desktop]# at -c 11
#!/bin/sh
# atrun uid=0 gid=0
# mail root 0
umask 22
cd /root/Desktop || {
  echo 'Execution directory inaccessible' >&2
  exit 1
}
${SHELL:-/bin/sh} << 'marcinDELIMITER74f1a456'
touch Desktop/file                   此为定时任务的设置
at -r 任务号 # 删除该延时任务
[root@server Desktop]# at -r 11    删除定时任务的设置
[root@server Desktop]# at -l                 再次查看是否已经删除 
[root@server Desktop]# 

  • 当延时任务有输出时候,并不是输出在终端上,而是以邮件的形式发送给任务的发起者。

执行date命令会输出当前时间:

[root@server Desktop]# date
Sat May 11 19:37:59 PDT 2019

使用超级用户延时执行date命令,并没有时间输出到该终端,并且该延时任务已经执行

[root@server Desktop]# date
Sat May 11 19:52:57 PDT 2019
[root@server Desktop]# at 19:54   设置date定时任务
at> date              date任务设置
at> <EOT>
job 12 at Sat May 11 19:54:00 2019
[root@server Desktop]# at -l               查看定时任务列表
12 Sat May 11 19:54:00 2019 a root
[root@server Desktop]# at -l               查看定时任务已经执行不存在,但是终端上并没有显示输出的时间
查看用户的邮件 mail -u 用户名
mail -u root# 查看超级用户的邮件
2 # 查看第二封邮件的详细信息
q # 退出
[root@server Desktop]# mail -u root            指定用户名称查看定时任务的设置
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/mail/root": 3 messages 3 new
>N  1 root                  Sat May 11 19:21  14/536   "Output from your job        6"
 N  2 root                  Sat May 11 19:29  14/535   "Output from your job        9"
 N  3 root                  Sat May 11 19:54  14/498   "Output from your job       12"
& 2           查看第二封邮件
Message  2:        
From [email protected]  Sat May 11 19:29:00 2019
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected]
Subject: Output from your job        9
To: [email protected]
Date: Sat, 11 May 2019 19:29:00 -0700 (PDT)
From: [email protected] (root)
Status: R
touch: cannot touch ‘Desktop/file’: No such file or directory             此为邮件的内容
& q                               退出
Held 3 messages in /var/mail/root
邮件的存放位置

/var/spool/mail/用户名 :每一个用户的邮件会被记录在以其用户名为名称的文件中
> /var/spool/mail/root 邮件清空

[root@server Desktop]# cat /var/spool/mail/root              查看到有邮件 
From [email protected]  Sat May 11 19:21:01 2019
Return-Path: <[email protected]>
X-Original-To: root
Delivered-To: [email protected] 
Received: by server.westos.com (Postfix, from userid 0)
 id 1049F30F0BC5; Sat, 11 May 2019 19:21:01 -0700 (PDT)
Subject: Output from your job        6
To: [email protected]
Message-Id: <[email protected]>
Date: Sat, 11 May 2019 19:21:01 -0700 (PDT)
From: [email protected] (root)
Status: O
[root@server Desktop]# > /var/spool/mail/root              清空邮件
[root@server Desktop]# cat /var/spool/mail/root             再次查看已经没有了邮件
[root@server Desktop]# 
延时任务的黑名单 /etc/at.deny

系统中默认存在,出现在黑名单中的用户将无法使用at设定延时任务。

在这里插入图片描述

延时任务的白名单 /etc/at.allow

系统中默认不存在,如果自己创建,黑名单失效;只有出现在白名单中的用户才能使用at设定延时任务,超级用户不受限制

在这里插入图片描述

系统定时任务

1 crontab 时间表示方式

在使用定时任务时候,需要给出执行任务的时间,时间格式如下:

*  *  *  *  *         #共五部分,依次表示 分钟  小时    天   月   星期

例如:

*    *    *    *     *           #表示每分钟
*/5  *    *    *     *          #表示每五分钟
*/5  */4    *    *     *          #表示每隔四小时,然后每五分钟
*/5  9-11    *    *     *          #表示9点到11点 每隔五分钟
*/5  9-11    10,20    9    5   #表示9月10号,20号以及每个星期五的9-11点每隔5分钟
*/5  11-12    *    *     6         #表示每周六11-12点 每隔五分钟
2 crontab
crontab -e -u #指定用户创建定时任务
[root@server ~]# crontab -e -u root
crontab: installing new crontab

在这里插入图片描述

crontab -l -u #查看指定用户的定时任务
[root@server ~]# crontab -l  -u root
*/5 * * * * touch  Desktop/file       查看指定任务的定时任务 
crontab -r -u # 取消指定用户的定时任务
[root@server ~]# crontab -r   -u root            取消用户的定时任务
[root@server ~]# crontab -l  -u root              再次查看已经没有了任务
no crontab for root
[root@server ~]# 

3系统控制crontab的服务

crontab由crond.service 服务控制。当该服务开启时,所设定的定时任务才能生效

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

创建/etc/cron.d/xxx 文件 ,在该文件中编写需要执行的定时任务
任务编写格式:* * * * * 用户(执行该任务的) 任务操作
在这里插入图片描述

5 crontab 的黑白名单
  • crontab 的黑名单

/etc/cron.deny 系统中默认存在,出现在黑名单中的用户将无法使用crontab设定定时任务。

在这里插入图片描述

crontab 的白名单

/etc/cron.allow 系统中默认不存在,如果自己创建,黑名单失效,只有出现在白名单中的用户才能使用crontab设定定时任务,超级用户不受限制。
在这里插入图片描述

  • crontab 的黑白名单不影响使用文件方式设定的定时任务,不论文件方式设定的定时任务的执行者在不在黑白名单中都可以成功执行设定的定时任务

系统中临时文件的管理

systemd-tmpfiles 系统用来创建、管理、删除临时文件的命令。
临时文件都存在有效期,当其有效期过了之后就可被清理,但是有效期没过就不能被清理。所以设置定时任务对系统的临时文件进行清理,在每次清理时过期的文件会被清理掉,只要设置合适的清理周期,就能保证系统中临时文件不会占用大量资源。
测试:
cd /usr/lib/tmpfiles.d/
/usr/lib/tmpfiles.d/ 中的.conf文件描述了 systemd-tmpfiles 如何创建、清理、删除临时文件和目录,记录了对临时文件的规范。

在这里插入图片描述

[root@server tmpfiles.d]# vim bobo.conf
[root@server tmpfiles.d]# cat bobo.conf
d /mnt/bobo  777  root root 10s 
[root@server tmpfiles.d]# 
systemd-tmpfiles --create /usr/lib/tmpfiles.d/*

编写一个临时目录的规范文件,并扫描该规范文件创造出该目录

systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*

使用该指令对临时文件的规范文件进行扫描,过期在这里插入图片描述的临时文件会被清理

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/liuchuang11/article/details/89980053