linux系统的延时任务和定时任务

0.系统的延时任务

[root@localhost mnt]# ll
total 0
[root@localhost mnt]# date
Mon Jan 21 10:39:18 CST 2019
[root@localhost mnt]# at 10:40           ##设定人物的执行时间
at> touch file{1..5}                     ##任务
at> <EOT>                                ##ctrl+d 发起任务
job 5 at Mon Jan 21 10:40:00 2019
[root@localhost mnt]# ls
file1  file2  file3  file4  file5

at -l #查看任务列表

多个任务可以同时等待被执行

[root@localhost mnt]# at -l
7	Mon Jan 21 10:48:00 2019 a root

at -c 任务号 ##查看任务的内容
at -r 任务号 ##取消任务的执行,一定要在任务执行之前取消

注意:当任务有输出的时候,输出会以邮件的形式发送给at任务的发起者

[root@localhost Desktop]# at now+1min
at> echo hello
at> <EOT>
job 12 at Mon Jan 21 11:00:00 2019
[root@localhost Desktop]# mail -u root      ##查看超级用户邮件
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/mail/root": 2 messages 2 new
>N  1 root                  Mon Jan 21 10:48  15/577   "Output from your job "
 N  2 root                  Mon Jan 21 11:00  14/501   "Output from your job "
& 2                        #查看第二封邮件
Message  2: 
From root@localhost.localdomain  Mon Jan 21 11:00:01 2019
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Subject: Output from your job       12
To: root@localhost.localdomain
Date: Mon, 21 Jan 2019 11:00:00 +0800 (CST)
From: root@localhost.localdomain (root)
Status: R

hello

& q                       ##q退出
Held 2 messages in /var/mail/root
You have mail in /var/mail/root

[root@localhost Desktop]# >/var/spool/mail/root   ##清空邮件
[root@localhost Desktop]# mail -u root
No mail for root
系统中的其他用户,也可以通过at命令发起任务的执行
[student@localhost ~]$ at 11:07
at> touch /home/student/hehe
at> <EOT>
job 14 at Mon Jan 21 11:07:00 2019
[student@localhost ~]$ at -l
14	Mon Jan 21 11:07:00 2019 a student


[student@localhost ~]$ at 11:08
at> touch /root/file         ##在任务的发起中一定要注意:不同用户的权限问题
at> <EOT>
job 15 at Mon Jan 21 11:08:00 2019
[student@localhost ~]$ at -l
15	Mon Jan 21 11:08:00 2019 a student
[student@localhost ~]$ ll
total 0
-rw-rw-r--. 1 student student  0 Jan 21 11:07 hehe
at任务的黑白名单

/etc/at.deny ##系统中默认存在,在此文件中不能执行at命令

[root@localhost Desktop]# vim /etc/at.deny 
[root@localhost Desktop]# cat /etc/at.deny 
student
[root@localhost Desktop]# su - student 
Last login: Mon Jan 21 11:05:18 CST 2019 on pts/0
[student@localhost ~]$ at 13:03
You do not have permission to use at.

/etc/at.allow ##系统中默认不存在,当文件出现普通用户不能执行at,只能在root中执行,/etc/at.deny这个文件失效

1.系统的定时任务

分钟 小时 天 月 周

  •   *      *      *      *     #每分钟
    

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

crontab -e ##root用户的定时任务
[root@localhost Desktop]# crontab -e            ##root用户的定时任务
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost Desktop]# crontab -l -u root    ##列出crontab任务
* * * * * touch /mnt/westos{1..3}
[root@localhost mnt]# ls
westos1  westos2  westos3  westos4  westos5  westos6  westos7  westos8  westos9

[root@localhost Desktop]# crontab -r -u root    ##删除crontab任务




[root@localhost mnt]# crontab -e -u student    ##root让普通用户执行定时任务
no crontab for student - using an empty one
crontab: installing new crontab

[root@localhost mnt]# crontab -l -u student    ##列出普通用户的定时任务
* * * * * touch /home/student/file{1..3}
文件的方式设定定时任务
[root@localhost mnt]# cd /etc/cron.d
[root@localhost cron.d]# vim file
[root@localhost cron.d]# cat file 
* * * * * root touch /mnt/redhet

非交互式

[root@localhost cron.d]# echo "* * * * * root rm -rf /mnt/*" > /etc/cron.d/file 
[root@localhost cron.d]# cat file 
* * * * * root rm -rf /mnt/*
在文件的方式定义crontab任务的时候,使用crontab -是看不到内容的

#以下目录只对超级用户可写

[root@localhost cron.d]# ll -d /etc/cron.d
drwxr-xr-x. 2 root root 83 Jan 21 14:26 /etc/cron.d

crontab的黑名单
/etc/cron.deny ##系统中默认存在,在此文件中出现的用户不能执行crontab

[root@localhost cron.d]# ll -d /etc/cron.deny 
-rw-------. 1 root root 0 Jan 28  2014 /etc/cron.deny

root@localhost cron.d]# vim /etc/cron.deny  ##将student添加上去
[root@localhost cron.d]# cat /etc/cron.deny 
student
[root@localhost cron.d]# su - student 
Last login: Mon Jan 21 13:02:17 CST 2019 on pts/0
[student@localhost ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
/etc/cron.allow ##系统中默认不存在,当文件创建出来的时候,普通用户不能执行

#只有在名单中的用户才可以使用

[root@localhost cron.d]# ll -d /etc/cron.allow
ls: cannot access /etc/cron.allow: No such file or directory
[root@localhost cron.d]# vim /etc/cron.allow    ##给里面添加用户
[root@localhost cron.d]# cat /etc/cron.allow 
student
[root@localhost cron.d]# su - student 
Last login: Mon Jan 21 14:40:48 CST 2019 on pts/0
[student@localhost ~]$ crontab -e      ##设定定时任务
no crontab for student - using an empty one
crontab: no changes made to crontab
[student@localhost ~]$ crontab -l    #查看
* * * * * touch /mnt/hello{1..3}

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

[root@localhost cron.d]# cd /usr/lib/tmpfiles.d/
[root@localhost tmpfiles.d]# vim westos.conf
[root@localhost tmpfiles.d]# cat westos.conf 
d               /mnt/westos        777 root  root    5s
目录              要建立的目录       权限 拥有者 所属组   存在5S以上才可以被清理

[root@localhost tmpfiles.d]# systemd-tmpfiles --create /usr/lib/tmpfiles.d/*    ##读取里面的所有文件并按照规则去建立目录
[root@localhost westos]# touch file{1..9}
[root@localhost westos]# ls
file1  file2  file3  file4  file5  file6  file7  file8  file9

等待5S

[root@localhost westos]# systemd-tmpfiles --clean /usr/lib/tmpfiles.d/*   ##按照规则清理目录中的文件
[root@localhost westos]# ll
total 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值