计划任务

一、计划任务

  • 计划任务主要是做一些周期性的任务,目前主要用途是定期备份数据

二、一次调度执行at

  • 语法格式
at <TIMESPEC>
	now +5min
	teatime tomorrow (teatime is 16:00)
	noon +4 days
	5pm august 3 2021

atq可以查看任务队列
at只可以执行一次就结束
  • 例1
[root@localhost ~]# systemctl start atd
[root@localhost ~]# systemctl enable atd
[root@localhost ~]# at now +1min
at> useradd zhangsan
at> <EOT> # Ctrl+D结束
job 1 at Thu Aug 27 15:35:00 2020
[root@localhost ~]# atq
1 Thu Aug 27 15:35:00 2020 a root
[root@localhost ~]# id zhangsan
uid=6671(zhangsan) gid=6671(zhangsan)=6671(zhangsan)
  • 例2
[root@localhost ~]# vi at.jobs
touch /root/`date +%F`.txt
useradd lisi
[root@localhost ~]# at now +1min < at.jobs
[root@localhost ~]# ls
2020-08-27.txt anaconda-ks.cfg at.jobs
[root@192 ~]# id lisi
uid=6672(lisi) gid=6672(lisi)=6672(lisi)
  • /etc/at.{allow,deny}控制用户是否能指向at任务

    • 白名单:/etc/at.allow默认不存在,只有该文件中的用户才能执行at命令

    • 黑名单:/etc/at.deny默认存在,拒绝该文件中用户执行at命令,而没有在at.deny文件中的使用者则可执行

    • 如果两个文件都不存在,只有root可以执行at命令

三、循环调度执行cron 用户级

  • cron 在使用之前必须要启动守护进程
[root@localhost ~]# systemctl start crond 
[root@localhost ~]# systemctl enable crond 
[root@localhost ~]# ps aux|grep crond 
root 6242 0.3 0.0 126380 1656 ? Ss 16:27 0:00 /usr/sbin/crond -n
  • crond 进程每分钟会处理一次计划任务
  • 存储位
[root@localhost ~]# ls /var/spool/cron
  • 管理命令
[root@localhost ~]# crontab -l # 列出当前用户所有计划任务 
[root@localhost ~]# crontab -r # 删除当前用户计划任务 
[root@localhost ~]# crontab -e # 编辑当前用户计划任务 
管理员可以使用 -u username,去管理其他用户的计划任务 
[root@localhost ~]# vi /etc/cron.deny # 这个文件中加入的用户名无法使用cron
  • cron 语法格式
分 时 日 月 星期 命令 
* 表示任何数字都符合 
0 2 * * * /run.sh # 每天的2点 
0 2 14 * * /run.sh # 每月14号2点 
0 2 14 2 * /run.sh # 每年2月14号2点 
0 2 * * 5 /run.sh # 每个星期5的2点 
0 2 * 6 5 /run.sh # 每年6月份的星期5的2点 
0 2 2 * 5 /run.sh # 每月2号或者星期5的2点 星期和日同时存在,那么就是或的关系 
0 2 2 6 5 /run.sh # 每年6月2号或者星期5的2点 

/5 * * * * /run.sh # 每隔5分钟执行一次 
0 2 1,4,6 * * /run.sh # 每月1号,4号,6号的2点 
0 2 5-9 * * /run.sh # 每月5-9号的2点 

* * * * * /run.sh # 每分钟 
0 * * * * /run.sh # 每整点 
* * 2 * * /run.sh # 每月2号的每分钟
[root@localhost ~]# crontab -e
0 2 * * * /run.sh 
# 每天2点执行run.sh这个文件

vim /run.sh
touch /run/`data +%F`.txt

chmod +x /run.sh

四、循环调度执行cron 系统级

  • 临时文件的清理 /tmp /var/tmp
  • 系统信息的采集 sar
  • 日志的轮转(切割) lgrotate
  • 通常不是由用户定义
  • 文件的位置
[root@localhost ~]# vim /etc/crontab # 默认没有定义任何计划任务 
[root@localhost ~]# ls /etc/cron.d # 定义的计划任务每个小时会执行 
0hourly sysstat 
[root@localhost ~]# cat /etc/cron.d/0hourly 
# Run the hourly jobs 
SHELL=/bin/bash 
PATH=/sbin:/bin:/usr/sbin:/usr/bin 
MAILTO=root 
01 * * * * root run-parts /etc/cron.hourly # 每小时01分以root身份执 行/etc/cron.hourly/目录下的所有脚本
  • crond 仅仅会执行每小时定义的脚本 /etc/cron.hourly
[root@localhost ~]# ls /etc/cron.hourly/ 
[root@localhost ~]# cat /etc/cron.hourly/0anacron 
/usr/sbin/anacron -s # anacron是用来检查是否有错过的计划任务需要被执行 
[root@localhost ~]# vi /etc/anacrontab 
1 5 cron.daily nice run-parts /etc/cron.daily #每天开机 5 分钟后就检查 /etc/cron.daily 目录内的文件是否被执行,如果今天没有被执行,那 就执行 
7 25 cron.weekly nice run-parts /etc/cron.weekly #每隔 7 天开机后 25 分钟检查 /etc/cron.weekly 目录内的文件是否被执行,如果一周内没有被 执行,就会执行 
©monthly 45 cron.monthly nice run-parts /etc/cron.monthly #每隔一个月开机后 45 分钟检查 /etc/cron.monthly 目录内的文件是否被执行,如果一个月内没 有被执行,那就执行
  • 15
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值