个人主页:💗wei_shuo的个人主页
🏀 Hello World !🏀
Linux 计划任务
介绍
大家平常都会有一些比如说:你每天固定几点起床?每天按时上班打卡、每月 15 号准时开工资、每年 2 月 14你俩口子某某纨念日等这些诸如此类,这些都是定时发生的。或者说是通俗点说:例行公事;还比如说我们还会遇到一些突发事件,临时几点过来加个班?刚好晚上几点聚个餐?
像上面这些情况,如果事少的话你大脑可以记住,如果事徆多,像老板经理董事长每天的工作安排,通常都是记在一些本上,或者闹铃提醒等。
那么,咱们的 LINUX 系统和上面的情况也很类似,我们也可以通过一些设置。来让电脑定时提醒我们该做什么事了。或者我们提前设置好,告诉电脑你几点做什么几点做什么,这种我们就叫它定时任务。而遇到一些需要执行的事情或任务。我们也可以通过命令来告诉电脑一会临时把这个工作给做一下。
总结:在我们 LINUX 中,我们可以通过 crontab 和 at 这两个东西来实现这些功能的
计划任务的作用:做一些周期性的任务,在生产中的主要用来定期备份数据
CROND:这个守护进程是为了周期性执行任务或处理等待事件而存在
任务调度分两种:系统任务调度,用户任务调度
计划任务的安排方式分两种:
# 一种是定时性的,也就是例行。就是每隔一定的周期就要重复来做这个事情
# 一种是突发性的,就是这次做完了这个事,就没有下一次了,临时决定,只执行一次的任务
at 计划任务的使用
语法格式:at 时间
服务:atd
# 安装 atd
[root@centos7 ~]# yum -y install at
# 开启 atd 服务,并加入开机自启
[root@centos7 ~]# systemctl start atd
[root@centos7 ~]# systemctl enable atd
# 查看是否是开机自启,如果弹出 enabled,说明开启此服务
[root@centos7 ~]# systemctl is-enabled atd
enabled
# 在 Centos6 查看开机启动服务
[root@centos6 ~]# chkconfig --list | grep atd #此命令在 centos7 上不能执行
实战-使用 at 创建计划任务
# 查看系统时间
[root@centos7 ~]# date
2020年 11月 27日 星期六 20:21:13 CST
# 注意:如果是上午时间,后面加上 am,比如 9:20am
[root@centos7 ~]# at 20:24
at> mkdir /opt/test #输入要执行的命令
at> touch /opt/test/a.txt
at> <EOT> #结束:ctrl+d
job 1 at Sat Nov 27 20:24:00 2021
# 查看计划任务
[root@centos7 ~]# at -l
1 Sat Nov 27 20:24:00 2020 a root
#或者
[root@centos7 ~]# atq
# 检查 at 计划任务运行结果
[root@centos7 ~]# ls -l /opt/test/
总用量 0
-rw-r--r--. 1 root root 0 11月 27 20:24 a.txt
[root@centos7 ~]# ls -l /opt/test/a.txt
-rw-r--r--. 1 root root 0 11月 27 20:24 /opt/test/a.txt
查看和删除 at 将要执行的计划任务
查看计划任务
# 这个查看,只能看到还没有执行的。如果这个任务已经开始执行或者执行完成了,是看不到的
[root@centos7 ~]# at -l
2 Sat Nov 27 20:30:00 2020 a root
任务编号 执行的时间 队列 执行者
# -c 打印任务的内容到标准输出, 查看 2 号计划任务具体内容
[root@centos7 ~]# at -c 2
# 查看定时任务内容
[root@centos7 ~]# ls /var/spool/at/
a0000301a091c2 spool
[root@centos7 ~]# tail -5 /var/spool/at/a0000301a091c2
at 计划任务的特殊写法
[root@ centos7 ~]# at 20:00 2018-10-1 # 在某天
[root@ centos7 ~]# at now +10min # 在 10 分钟后执行
[root@ centos7 ~]# at 17:00 tomorrow # 明天下午 5 点执行
[root@ centos7 ~]# at 6:00 pm +3 days # 在 3 天以后的下午 6 点执行
[root@ centos7 ~]# at 23:00 < a.txt
删除 at 计划任务
语法: atrm 任务编号
[root@centos7 ~]# at -l
3 Sat Nov 27 20:50:00 2020 a root
[root@centos7 ~]# atrm 3
[root@centos7 ~]# at -l
crontab 定时任务的使用
crond 命令定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作
cron 是一个 linux 下的定时执行工具,可以在无需人工干预的情况下运行作业。
系统执行的工作:系统周期性所要执行的工作,如更新 whatis 数据库 updatedb 数据库,日志定期切割,收集系统状态信息,/tmp 定期清理
# 启动 crond 服务
[root@centos7 ~]# systemctl start crond
[root@centos7 ~]# systemctl enable crond
[root@centos7 ~]# systemctl status crond
cron 选项介绍
crontab 的选项:
[root@centos7 ~]# crontab -u demo #指定 demo 用户的 cron 服务
[root@centos7 ~]# crontab -l #列出弼前用户下的 cron 服务的详绅内容
[root@centos7 ~]# crontab -u demo -l #列出指定用户 demo 下的 cron 服务的详绅内容
[root@centos7 ~]# crontab -r #删除 cron 服务
[root@centos7 ~]# crontab -e #编辑 cron 服务
# 例如:
[root@centos7 ~]# crontab -u root -l # root查看自己的 cron 计划任务
[root@centos7 ~]# crontab -u demo -r # root 想删除 demo 的 cron 计划任务
cron -e 编辑时的语法
星期日用 0 或 7 表示
一行对应一个任务,特殊符号的含义
创建计划任务
# 每天凌晨 2 点 1 分开始备份数据
# 添加计划任务
[root@centos7 ~]# crontab -e
1 2 * * * tar zcvf /opt/grub2.tar.gz /boot/grub2
# 黑客:以非 root 用户添加计划任务。最好使用已经存在系统用户添加。这里使用 bin 用户来添加
[root@centos7 ~]# crontab -u bin -e
1 * * * * echo "aaaaaaa" >> /tmp/bin.txt
[root@centos7 ~]# crontab -u bin -l
# 如何排查所有用户的计划任务?
# 做黑客要有一个很扎实的基础,还要有很好的思维
# 注:所有用户的计划任务,都会在/var/spool/cron/下产生对应的文件
[root@centos7 ~]# ll /var/spool/cron/
# 所以后期可以使用这一招排查,黑客是否在你的机器中安装了定时任务
系统级别的计划任务
[root@centos7 ~]# ll /etc/crontab
# 这个是系统任务调度的配置文件
-rw-r--r--. 1 root root 451 6月 10 2014 /etc/crontab
[root@centos7 ~]# vim /etc/crontab
SHELL=/bin/bash #指定操作系统使用哪个 shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin #系统执行命令的搜索路径
MAILTO=root #将执行任务的信息通过邮件发送给 xx 用户
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
#也可以直接在/etc/crontab 中添加计划任务
[root@centos7 ~]# ls /etc/cron #按两下 tab 键
cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ crontab cron.weekly/
# 注:
cron.d/ #系统自动定期需要做的任务,但是又不是按小时,按天,按星期,按月来执行的,那么就放在这个目录下面。
cron.deny #控制用户是否能做计划任务的文件;
cron.monthly/ #每月执行的脚本;
cron.weekly/ #每周执行的脚本;
cron.daily/ #每天执行的脚本;
cron.hourly/ #每小时执行的脚本;
crontab #主配置文件 也可添加任务;
实战-常见的计划任务写法和案例
#每天晚上 21:00 重启 apache
0 21 * * * /etc/init.d/httpd restart
#每月 1、10、22 日的 4 : 45 重启 apache。
45 4 1,10,22 * * /etc/init.d/httpd restart
#每月 1 到 10 日的 4 : 45 重启 apache。
45 4 1-10 * * /etc/init.d/httpd restart
#每隔两天的上午 8 点到 11 点的第 3 和第 15 分钟重启 apach
3,15 8-11 */2 * * /etc/init.d/httpd restart
#晚上 11 点到早上 7 点之间,每隔一小时重启 apach
0 23-7/1 * * * /etc/init.d/apach restart
#周一到周五每天晚上 21:15 寄一封信给 root@demo:
15 21 * * 1-5 mail -s "hi" root@demo < /etc/fstab
crontab 不支持每秒,那么每10秒执行一次脚本,怎么写?
[root@centos7 ~]# # crontab -e
* * * * * /bin/date >>/tmp/date.txt
* * * * * sleep 10; /bin/date >>/tmp/date.txt
* * * * * sleep 20; /bin/date >>/tmp/date.txt
* * * * * sleep 30; /bin/date >>/tmp/date.txt
* * * * * sleep 40; /bin/date >>/tmp/date.txt
案例要求
每天 2:00 备份/etc/目录到/tmp/backup 下面
将备份命令写入一个脚本中
每天备份文件名要求格式: 2020-08-19_etc.tar.gz
在执行计划任务时,不要输出任务信息
存放备份内容的目录要求只保留三天的数据
[root@centos7 ~]#mkdir /tmp/backup
[root@centos7 ~]#tar zcf etc.tar.gz /etc
[root@centos7 ~]#find /tmp/backup -name “*.tar.gz” -mtime +3 -exec rm -rf {}\;
----------------------------------------------------------------------------------
[root@centos7 ~]# crontab -l
0 2 * * * /root/backup.sh & > /dev/null
[root@centos7 ~]# cat backup.sh
#!/bin/bash
find /tmp/backup -name "*.tar.gz" -mtime +3 -exec rm -f {}\;
#find /tmp/backup -name "*.tar.gz" -mtime +3 -delete
#find /tmp/backup -name "*.tar.gz" -mtime +3 |xargs rm -f
tar zcf /tmp/backup/`date +%F`_etc.tar.gz /etc
注:工作中备份的文件不要放到 /tmp, 因为过一段时间,系统会清空 /tmp 目录!!