Blog13@linux延迟、定时任务介绍及临时文件时间更改

来源:互联网 发布:mac os sierra 卡 编辑:程序博客网 时间:2024/06/08 10:14

摘要:延迟、定时任务介绍及临时文件时间更改

一、at延迟执行任务命令(临时性)

1.at命令执行流程

#at 2:00 tomorrowat>Ctrl+Dat time 中时间的一些表示方法:例:at now+5min at now+5hoursat now+5daysat now+5weeksat now+5monthsat now+5yearsat midnightat 10:30pmat 22:30 12/31/2017

2.at命令的参数详情

at -l / atq                 #列出延迟任务idat -r  任务id /atrm+任务id   #删除此id 任务at -m                       #让无输出的命令产生邮件at -M            #让有输出的命令不产生邮件at -c  任务id     #查看此id任务的执行动作at -f  文件       #用文件内记录的命令作为延迟任务的执行命令注:一般只有输出的命令才会有邮件


实操如下:

############atq,at -l/-r/-c3########### [root@client ~]# at 2:00 tomorrow --->明日两点执行如下命令at> touch file{1..3} at> dateat> <EOT>[root@client ~]# atq      1    Tue Oct 31 02:00:00 2017 a root[root@client ~]# at -l1    Tue Oct 31 02:00:00 2017 a root[root@client ~]# at -c 1...(省略中间内容)touch file{1..3}date[root@client ~]# at -r 1[root@client ~]# atq
##################at -m/M#################注:已知touch file /mnt/file1为无输出命令date为有输出命令[root@client ~]# at now+1minat> touch /mnt/file1at> dateat> <EOT>job 6 at Mon Oct 30 09:24:00 2017[root@client ~]# mailHeirloom Mail version 12.5 7/5/10.  Type ? for help."/var/spool/mail/root": 1 message 1 new>N  1 root                  Mon Oct 30 09:24  14/506  -->mail邮件中只显示了date执行过的命令& qHeld 1 message in /var/spool/mail/rootYou have mail in /var/spool/mail/root[root@client ~]# >/var/spool/mail/root -->清空mail邮件[root@client ~]# at  now+1minat> touch /mnt/fileat> <EOT>job 17 at Mon Oct 30 10:18:00 2017[root@client ~]# at  now+1minat> dateat> <EOT>job 18 at Mon Oct 30 10:18:00 2017[root@client ~]# mailHeirloom Mail version 12.5 7/5/10.  Type ? for help."/var/spool/mail/root": 1 message 1 new>N  1 root                  Mon Oct 30 10:18  14/503  & q               ---------->邮件中只了显示date命令的执行 Held 1 message in /var/spool/mail/root[root@client ~]# atq[root@client ~]# >/var/spool/mail/root[root@client ~]# at -m now+1minat> touch /mnt/file6at> <EOT>job 19 at Mon Oct 30 10:19:00 2017[root@client ~]# at now+1minat> dateat> <EOT>job 20 at Mon Oct 30 10:19:00 2017[root@client ~]# mailHeirloom Mail version 12.5 7/5/10.  Type ? for help."/var/spool/mail/root": 2 messages 2 new>N  1 root                  Mon Oct 30 10:19  13/474    N  2 root                  Mon Oct 30 10:19  14/503   & q     --------->邮件中显示了date及touch /mnt/file6 的执行Held 2 messages in /var/spool/mail/root[root@client ~]# >/var/spool/mail/root[root@client ~]# at -M now+1minat> touch file1at> <EOT>job 21 at Mon Oct 30 10:24:00 2017[root@client ~]# at -M now+1minat> dateat> <EOT>job 22 at Mon Oct 30 10:25:00 2017[root@client ~]# atq[root@client ~]# mail  -->mail邮件中无执行历史

注:若发现在/mnt/下建立文件无显示,请查看并修改/mnt权限

3.at用户黑白名单

##用户黑名单默认情况下,系统中是存在用户黑名单的,这个名单的路径如下
/etc/at.deny  ##用户黑名单,此名单默认为空,并且在白名单不存在时生效


##at用户白名单默认系统中是不存在at的白名单的,但是当白名单出现,系统用除名单中出现的用户以及root用户其他用户均不能执行at命令

 touch /etc/at.allow  ##建立用户白名单,白名单一旦出现,黑名单失效
  vim /etc/at.allow
注:删除/etc/at.allow白名单,其即会失效

二、cornat定时执行任务命令(永久性)

1.这个程序是由cond.service 服务提供,且写入程序的时间和事件是永久保存的

2.cron设定方式

法一

crontab -e -u用户名称(可以不写)分钟 小时    天    月    周         事件58   06      *     *     *     rm -rf /mnt/*  #每天6:58分删除/mnt中的内容58   06     1,15   *     *     rm -rf /mnt/*  #每月一号和每月15号的6:58分删除/mnt中的内容58   06     1-15   *     *     rm -rf /mnt/*  #每月一号到每月15号的6:58分删除/mnt中的内容58   06     1,15   3     *     rm -rf /mnt/*  #3月的1号和15号的6:58分删除/mnt中的内容58   06     1,15   3     3     rm -rf /mnt/*  #3月的1号和15号及3月的所有周三的6:58分删除/mnt中的内容58   06-17   *     *     *     rm -rf /mnt/*  #6-17点,每隔1小时*/30 06-17   *     *    1-5    rm -rf /mnt/*  #周一到周五的早上6点到下午5点每隔半小时执行


法二:

文件方式定义crontab

1)

注:/var/spool/cron/*  ##cron任务记录文件的存放位置
vim /var/spool/cron/用户名称  #以某个用户的身份执行cron,文件名称必
须和用户名一致
分    时    天    月    周    动作


2)

vim /etc/cron.d/文件  #设定多个用户身份执行cron
分    时    天    月    周    用户    动作
实操:
[root@client ~]# vim /etc/cron.d/RS

[root@client ~]# watch -n 1 ls -l /mnt

3)脚本方式

cat cron.sh
#!/bin/bash
cat >> /etc/cron.d/westos <<Sunshine
*****root touch /tmp/file{1..3}
*****student touch /tmp/file{4..6}
Sunshine

实操:
[root@client cron.d]# vim cron.sh

[root@client cron.d]# sh cron.sh
[root@client cron.d]# watch -n 1 ls -l /mnt

3.corntab的基本参数详情

crontab -l #列出当前用户的cron用户
crontab -r #删除当前用户的cron用户

实操如下:
[root@client ~]# crontab -e
crontab: installing new crontab
[root@client ~]# crontab -e -u student
crontab: installing new crontab
[root@client ~]# crontab -l
* * * * * touch /mnt/file{1..3}
[root@client ~]# crontab -u student -l
* * * * * rm -rf /mnt/file{1..3}


4.cron的黑白名单

默认情况下,系统中只存在黑名单,当白名单建立,黑名单失效

#黑名单
/etc/cron.deny  ##在此名单下的用户不能执行crontab
#白名单
/etc/cron.allow   #用户白名单,当此名单出现,除root用户及名单中的用户其它的用户不能执行crontab

三、临时文件时间修改

关于/tmp文件,想必大家都有所耳闻,
其作用:让一般用户或者正在执行的程序暂时存放文件的地方。这个目录任何人都可以访问,所以需要定期的清理。当然重要的数据不可放置在此目录。因为FHS甚至建议在开机时,应该要将/tmp下的数据清理一下。
为了能够更好地管理临时文件的存留,其当然有固定的存在时长。那么,在此时间内,其文件不可被清理。而这个存在的时长也可被更改。其更改在如下配置文件:

[root@client ~]# vim /usr/lib/tmpfiles.d/tmp.conf

那么我们是否可以将自己建立的一些临时文件存放在固定的目录内?
答案是肯定的。
为了方便区分并管理自己建立的这些临时文件存放的目录,

实际操作过程如下:

[root@client ~]# vim /usr/lib/tmpfiles.d/westos.conf


[root@client ~]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*
[root@client ~]# cd /mnt/westos/
[root@client westos]# touch file{1..3}


[root@client westos]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*


[root@client westos]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*