摘要:延迟、定时任务介绍及临时文件时间更改
实操如下:
############atq,at -l/-r/-c3########### [root@client ~]# at 2:00 tomorrow --->明日两点执行如下命令 at> touch file{1..3} at> date at> <EOT> [root@client ~]# atq 1 Tue Oct 31 02:00:00 2017 a root [root@client ~]# at -l 1 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+1min at> touch /mnt/file1 at> date at> <EOT> job 6 at Mon Oct 30 09:24:00 2017 [root@client ~]# mail Heirloom 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执行过的命令 & q Held 1 message in /var/spool/mail/root You have mail in /var/spool/mail/root [root@client ~]# >/var/spool/mail/root -->清空mail邮件 [root@client ~]# at now+1min at> touch /mnt/file at> <EOT> job 17 at Mon Oct 30 10:18:00 2017 [root@client ~]# at now+1min at> date at> <EOT> job 18 at Mon Oct 30 10:18:00 2017 [root@client ~]# mail Heirloom 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+1min at> touch /mnt/file6 at> <EOT> job 19 at Mon Oct 30 10:19:00 2017 [root@client ~]# at now+1min at> date at> <EOT> job 20 at Mon Oct 30 10:19:00 2017 [root@client ~]# mail Heirloom 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+1min at> touch file1 at> <EOT> job 21 at Mon Oct 30 10:24:00 2017 [root@client ~]# at -M now+1min at> date at> <EOT> job 22 at Mon Oct 30 10:25:00 2017 [root@client ~]# atq [root@client ~]# mail -->mail邮件中无执行历史注:若发现在/mnt/下建立文件无显示,请查看并修改/mnt权限
##at用户白名单默认系统中是不存在at的白名单的,但是当白名单出现,系统用除名单中出现的用户以及root用户其他用户均不能执行at命令
touch /etc/at.allow ##建立用户白名单,白名单一旦出现,黑名单失效 vim /etc/at.allow注:删除/etc/at.allow白名单,其即会失效
法二:
文件方式定义crontab
注:/var/spool/cron/* ##cron任务记录文件的存放位置 vim /var/spool/cron/用户名称 #以某个用户的身份执行cron,文件名称必 须和用户名一致 分 时 天 月 周 动作
vim /etc/cron.d/文件 #设定多个用户身份执行cron 分 时 天 月 周 用户 动作 实操: [root@client ~]# vim /etc/cron.d/RS [root@client ~]# watch -n 1 ls -l /mnt
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
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}
默认情况下,系统中只存在黑名单,当白名单建立,黑名单失效 #黑名单 /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/*