linux下系统的延时任务,定时任务,临时文件的管理方式

1.系统的延时任务

at -c 任务号 #查看任务的内容
at -r 任务号 #取消任务的执行
####touch /mnt/file{1..9}
[root@server94 ~]# date
Sat Jan 19 20:52:37 EST 2019
[root@server94 ~]# at 20:54                   #设定任务的执行时间
at> touch file                                #任务的动作
at> <EOT>                                     #ctrl+d 发起任务
job 1 at Sat Jan 19 20:54:00 2019
[root@server94 ~]# at -l                      #查看任务列表
1	Sat Jan 19 20:54:00 2019 a root
多个任务可以同时等待被执行
[root@server94 ~]# at 21:00
at> touch 1
at> <EOT>
job 2 at Sat Jan 19 21:00:00 2019
[root@server94 ~]# at 21:01 
at> touch 2
at> <EOT>
job 3 at Sat Jan 19 21:01:00 2019
[root@server94 ~]# at -l
2	Sat Jan 19 21:00:00 2019 a root
3	Sat Jan 19 21:01:00 2019 a root

注意:当任务有输出的时候,输出会以邮件的形式发送给任务的发起者
[root@server94 ~]# >/var/spool/mail/root   #清空邮件
[root@server94 ~]# mail -u root            #查看root用户的邮件
at任务的黑白名单
1.黑名单:
/etc/at.deny   #系统中默认存在,在此文件中出现的用户不能执行at命令
[root@server94 ~]# ll /etc/at.deny 
-rw-r--r--. 1 root root 1 Jan 29  2014 /etc/at.deny
[root@server94 ~]# vim /etc/at.deny 
[root@server94 ~]# cat /etc/at.deny 
student
[student@server94 ~]$ at 10:11
You do not have permission to use at.
白名单:
/etc/at.allow #系统中默认不存在,当文件出现,普通用户不能执行at,只能在名单
中出现的用户可以,/etc/at.deny这个文件失效

2.系统的定时任务

分钟 小时 天 月 周
* * * * *     #每分钟
*/2 * * * *   #每两分钟
*/2 09-17 * * *      #早9—晚5期间每两分钟
*/2 09-17 * 3,5 5   #3月和5月每周五
*/2 09-17 * * 5      #每周五早9晚5
[root@server94 ~]# crontab -e            #root用户的定时任务
[root@server94 ~]# crontab -r -u root    #删除crontab
[root@server94 ~]# crontab -l -u roo     #列出crontab任务
[root@server94 ~]# crontab -e -u student #root让普通用户执行定时任务

文件的方式设定定时任务
[root@server94 ~]# cd /etc/cron.d
[root@server94 cron.d]# vim file(文件名称任意)

 时间      用户      动作
* * * * * username action  # 某某用户执行什么动作

####在文件的方式定义crontab任务的时候,使用crontab -l是看不到内容的
#以下目录之对超级用户可写
[root@server94 cron.d]# ll -d /etc/cron.d
drwxr-xr-x. 2 root root 72 Jan 22 16:32 /etc/cron.d
crontab的黑白名单(定时任务)
黑名单:
/etc/cron.deny #系统中默认存在,在此文件中出现的用户不能执行crontab
[root@server94 cron.d]# ll -d /etc/cron.deny 
-rw-------. 1 root root 0 Jan 27  2014 /etc/cron.deny
[root@server94 cron.d]# vim /etc/cron.deny 
[root@server94 cron.d]# cat /etc/cron.deny 
student
[student@server94 ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
白名单:
/etc/cron.allow #系统中默认不存在,当文件创建出来的时候,普通用户不能执行,只有在名单中的用户可以使用
[root@server94 cron.d]# ll -d /etc/cron.allow
ls: cannot access /etc/cron.allow: No such file or directory
[root@server94 cron.d]# vim /etc/cron.allow
[root@server94 cron.d]# cat /etc/cron.allow
student
[student@server94 ~]$ crontab -e
crontab: installing new crontab

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

[root@server94 ~]# cd /usr/lib/tmpfiles.d/
[root@server94 tmpfiles.d]# vim westos.conf
文件内容:
d     /mnt/westos  777   root   root    5s
目录   要建立的目录  权限  拥有者  所属组  
[root@server94 ~]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*  #读取里面的所有文件并按规则去建立目录
[root@server94 ~]# cd /mnt/westos
[root@server94 ~]# tounch file{1..p}
等待5s
[root@server94 ~]# systemd-tmpfiles --clean  /usr/lib/tmpfiles.d/*  #按照滚则清理目录中的文件

猜你喜欢

转载自blog.csdn.net/weixin_44320876/article/details/86603540