linux中的定时任务及延时任务

(一)延时任务


一.at命令的基本用法


1.基本格式

[root@really mnt]# at now+1min     ##at  +指定时间(时时:分分)/ at + now+数字min
at> touch /mnt/file1               ##输入希望执行的命令 
at> <EOT>                          ##ctrl+d 执行
job 1 at Wed Nov  1 22:17:00 2017


2.at -l        #列出延迟任务id


3.at -r    任务id    #删除此id的任务


4.at -c    任务id    #查看此id的延迟任务的执行动作


5.at -f    文件     #用文件内记录的命令作为延迟任务的执行命令


6.at -m        #让无是出的命令产生邮件

7.at -M        #让有输出的命令不产生邮件



二.at命令的使用控制

默认系统中全部用户都有执行at命令的权力那么如何设定at命令的黑白名单:


1.用户黑名单

默认情况下,系统中是存在用户黑名单的

(1)这个名单的路径
          /etc/at.deny        ##用户黑名单,此名单默认为空,并且在白名单不存在时生效

vim /etc/at.deny
student                               ##student使用at命令的权力被禁止

[student@client ~]$ at now+1min        
You do not have permission to use at.    ##student不能执行at命令


2.at命令的白名单

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

(1)[root@really mnt]# touch /etc/at.allow        ##建立用户白名单,当白名单出现,黑名单失效

[root@really mnt]# su - student
Last login: Wed Nov  1 22:54:25 CST 2017 on pts/2
[student@really ~]$ at 22:55
You do not have permission to use at.           
[student@really ~]$ exit
logout

[root@really mnt]# su - kiosk 
Last login: Wed Nov  1 23:00:50 CST 2017 on pts/2
[kiosk@really ~]$ at 22:55
You do not have permission to use at.


(2)
[root@really mnt]# vim /etc/at.allow         #在白名单中添加用户,那么student用户可以执行at
[root@really mnt]# cat /etc/at.allow
student

[root@really mnt]# su - student 
Last login: Wed Nov  1 23:01:16 CST 2017 on pts/2
[student@really ~]$ at 23:02
at> <EOT>
job 11 at Thu Nov  2 23:02:00 2017
[student@really ~]$ exit
logout
[root@really mnt]# su - kiosk 
Last login: Wed Nov  1 23:01:42 CST 2017 on pts/2
[kiosk@really ~]$ at 23:23
You do not have permission to use at.          #student执行at成功,但测试kiosk依然不可以







(二)定时任务



一.cron基础用法


1.这个程序是由crond.service 服务提供



2.写入程序的时间和事件是永久保存的


3.cron设定方式

crontab -e -u 用户名称(可以不写)

格式
分钟  小时   天       月     周     事件
58    06    *        *     *     rm -fr /mnt/*     #每天6:58分删除mnt中的所有内容
58    06    1,15    *     *     rm -fr /mnt/*     #每月1号和每月15号的6:58分删除mnt中的所有内容
58    06    1-15     *     *     rm -fr /mnt/*     #每月1号到每月15号的6:58分删除mnt中的所有内容
58    06    1,15    3     *     rm -fr /mnt/*     #3月1号和15号的6:58分删除mnt中的所有内容
58    06    1,15     3     3     rm -fr /mnt/*     #3月1号和15号以及3月的所有周三
58    06-17   *      *     *     rm -fr /mnt/*     #6-17点,每个小时的58分
*/30  06-17   *      *     1-5   rm -fr /mnt/*     #周一到周五的早上6点到下午5点每隔半小时执行


4.crontab -l        ##列出当前用户的cron任务



5.crontab -r        ##删除当前用户的cron任务


6./var/spool/cron/*    ##cron任务记录文件存放位置




二.cron的黑白名单

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


1.黑名单

[root@really ~]# vim /etc/cron.deny                ##在此名单中的用户不能执行crontab
You have new mail in /var/spool/mail/root
[root@really ~]# cat /etc/cron.deny
student                                                               ##student用户不能执行crontab


[root@really ~]# su - student 
Last login: Wed Nov  1 23:29:18 CST 2017 on pts/2
[student@really ~]$ crontab -e
You (student) are not allowed to use this program (crontab)                  ##student执行crontab被禁止
See crontab(1) for more information


2.白名单

[root@really ~]# touch /etc/cron.allow                                                 #建立白名单,当此名单出现,除root用户及名单中的用户其他用户不能执行crontab

[root@really ~]# su - student
Last login: Thu Nov  2 09:12:46 CST 2017 on pts/1
[student@really ~]$ crontab -e
You (student) are not allowed to use this program (crontab)
See crontab(1) for more information
[student@really ~]$ exit
logout

[root@really ~]# su - kiosk 
Last login: Thu Nov  2 06:52:53 CST 2017 on :0
Last failed login: Thu Nov  2 09:02:45 CST 2017 from :0 on :0
There was 1 failed login attempt since the last successful login.
[kiosk@really ~]$ crontab -e
You (kiosk) are not allowed to use this program (crontab)
See crontab(1) for more information
[kiosk@really ~]$ exit
logout


[root@really ~]# vim /etc/cron.allow                             
You have new mail in /var/spool/mail/root
[root@really ~]# cat /etc/cron.allow
student                                                                            #student可以执行crontab

[root@really ~]# su - student 
Last login: Thu Nov  2 09:15:52 CST 2017 on pts/1
[student@really ~]$ crontab -e                                      #测试 student可以执行
no crontab for student - using an empty one
crontab: installing new crontab
[student@really ~]$ exit
logout

[root@really ~]# su - kiosk 
Last login: Thu Nov  2 09:16:07 CST 2017 on pts/1
[kiosk@really ~]$ crontab -e                                                          #测试 kiosk仍然不可以执行
You (kiosk) are not allowed to use this program (crontab)
See crontab(1) for more information




三.文件方式定义crontab

1.vim /var/spool/cron/用户名称            #以某个用户身份执行cron,文件民称必须和用户名称一致

格式:
分    时    天    月    周    动作



2.vim /etc/cron.d/文件                #设定多个用户身份执行cron

格式:
<分>  <时>  <天>  <月>  <周>  <用户>  <动作>


或直接多行录入




四.系统中的cron任务

1./etc/cron.daily/        ##系统每天做执行的程序


2./etc/cron.hourly/        ##系统每小时执行的程序


3./etc/cron.monthly/        ##系统每月执行的程序


4./etc/cron.weekly/        ##系统每星期执行的程序


































  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值