简介
什么是计划任务?
提前计划某个时间点做某件事情 --》定时定点去完成某个任务
闹铃
脚本: 帮助我们去完成某些事情的工具
计划任务: 帮助我们去执行脚本
不需要人去执行了,提升了工作效率,解放了人力
linux 系统则是由 cron (crond) 这个系统服务来控制的。Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的。另 外, 由于使用者自己也可以设置计划任务,所以, Linux 系统也提供了使用者控制计划任务的命令 :crontab 命令。
crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务
文件 /etc/crontab
[root@sanchuang ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=HOME=/
# run-parts
51 * * * * root run-parts /etc/cron.hourly
24 7 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
前 四行是用来配置crond任务运行的环境变量,第一行SHELL变量指定了系统要使用哪个shell,这里是bash,第二行PATH变量指定了系统执行 命令的路径,第三行MAILTO变量指定了crond的任务执行信息将通过电子邮件发送给root用户,如果MAILTO变量的值为空,则表示不发送任务 执行信息给用户,第四行的HOME变量指定了在执行命令或者脚本时使用的主目录。第六至九行表示的含义将在下个小节详细讲述。这里不在多说。
anacron
anacron服务是cron服务的补充程序,起到查漏补缺的作用
anacron服务
弥补cron在系统关机后不能执行计划任务的问题,等系统开机后将没有执行的计划任务执行一遍。
不能替代cron
按天、周或月为单位去检查系统未进行的cron任务
/var/spool/anacron
服务名称:/etc/init.d/anacrond
开机时自动运行,然后将未执行的计划任务执行一遍后,anacron 就会自动停止
[root@sanchuang cron.daily]# cat /etc/anacrontab
计划任务相关的文件:
配置文件: /etc/crontab
日志文件: /var/log/cron
存放每个用户的计划任务文件: /var/spool/cron/用户名
操作系统自身需要的计划任务: /etc/{cron.daily,cron.hourly,cron.weekly,cron.monthly}
进程
案例:
如何做到让一个程序每间隔1秒去做一件事情?
程序里面使用while循环,每间隔1秒钟执行一次 sleep命令
shell脚本
[root@sanchuang lianxi]# cat create_dir_v2.sh
#!/bin/bash
#新建test目录
mkdir -p /lianxi/test
num=1 #num变量的初始值
while :
do
mkdir /lianxi/test/sanchuang$(date +%Y%m%d%H%M%S)
sleep 1 #暂停1秒
#记录已经创建了多少个文件夹了,搞一个日志文件记录
echo $((num++)) >/lianxi/test/num.log #num++ 等同于 num=num +1
done
python脚本
[root@sanchuang sanchuang]# cat create_dir_v4.py
#!/usr/bin/python3
#导入os time datetime 库
import os
import time
import datetime
#判断/lianxi/test4文件夹是否存在
#os.path.exists() 判断文件是否存在
#os.mkdir() 新建文件夹
if not os.path.exists("/lianxi/test4"):
os.mkdir("/lianxi/test4")
#切换路径到/lianxi/test4
os.chdir("/lianxi/test4")
num = 0
#每一秒新建一个文件夹
while True:
#获得当前的时间
ctime = datetime.datetime.now().__format__("%Y%m%d%H%M%S")
#拼接文件名 sanchuang+时间
filename = f"sanchuang{ctime}"
#新建文件夹
os.mkdir(filename)
#暂停1秒
time.sleep(1)
#统计新建的文件夹的数量
num = num + 1
#将num变量的值写到文件/lianxi/test4/num.log里
with open("/lianxi/test4/num.log","w") as f:
f.write(f"{num}\n")
定制计划任务
时间设置非常灵活
命令
crontab
计划任务的相关命令:
crontab -e edit编辑计划任务
crontab -l 查看计划任务 list
crontab -l -u chenlin 查看chenlin这个用户的计划任务
计划任务存放的目录 : /var/spool/cron
样例
每天的23:00备份一次,使用计划任务执行
备份脚本: /lianxi/backup_log.sh
计划任务的相关命令:
crontab -e edit编辑计划任务
crontab -l 查看计划任务 list
crontab 计划任务
[root@sanchuang lianxi]# crontab -e
第1步:按i进入计划任务的编辑模式,输入下面的内容
0 23 * * * bash /lianxi/backup_log.sh
第2步:按ESC,再输入:wq 退出并且保存
[root@sanchuang lianxi]# crontab -l 查看计划任务
0 23 * * * bash /lianxi/backup_log.sh
计划任务执行脚本的时候,是在后台执行的,人看不到
如何知道计划任务是否执行?
1.直接看效果
2.看日志文件 crontab的日志: 记录哪些计划任务的创建、执行、修改、查看等操作
/var/log/cron
[root@sanchuang log]# tail -f cron
Mar 16 15:06:35 sanchuang crontab[22483]: (root) LIST (root) 查看
Mar 16 15:06:49 sanchuang crontab[22484]: (root) BEGIN EDIT (root) 编辑
Mar 16 15:07:07 sanchuang crontab[22484]: (root) REPLACE (root) 修改
Mar 16 15:07:07 sanchuang crontab[22484]: (root) END EDIT (root) 结束编辑
Mar 16 15:07:25 sanchuang crontab[22486]: (root) LIST (root)
Mar 16 15:08:01 sanchuang crond[22151]: (root) RELOAD (/var/spool/cron/root) 加载计划任务开始执行
Mar 16 15:08:01 sanchuang CROND[22491]: (root) CMD (bash /lianxi/sanchuang/create_dir.sh)
Mar 16 15:08:01 sanchuang CROND[22489]: (root) CMDOUT (tar: 从成员名中删除开头的“/”) 脚本的执行输出效果
CROND[22491] CROND 是进程 [22491] 是crond的进程号 pid号
CMD 表示执行命令 command
[root@sanchuang log]# ps aux|grep crond
root 22151 0.0 0.1 36332 3624 ? Ss 13:08 0:00 /usr/sbin/crond -n
root 22518 0.0 0.0 12320 976 pts/1 R+ 15:10 0:00 grep --color=auto crond
计划任务的顺风车: linux系统本身也有一些任务需要定时完成的
例如:日志文件的轮转--》每隔一段时间产生一个新的日志文件
updatedb --》locate 自动每天更新一次
/var/spool/cron 是存放用户的计划任务的
linux系统的后台的工作开展需要的计划任务存放在哪里?
/etc/下的这些文件夹里可以存放linux系统的计划任务
cron.d/ cron.daily/ cron.deny cron.hourly/ cron.monthly/ crontab cron.weekly/
cron.d/ 存放计划任务的目录
cron.daily/ 存放每天需要执行的任务
[root@zabbix-client-1 cron.daily]# pwd
/etc/cron.daily
[root@zabbix-client-1 cron.daily]# ls
logrotate man-db.cron mlocate
[root@zabbix-client-1 cron.daily]#
cron.hourly/ 存放每小时需要执行的任务
cron.monthly/ 存放每月需要执行的任务
cron.weekly/ 存放每周需要执行的任务
cron.deny 存放拒绝谁执行计划任务
[root@sanchuang cron.d]# cat /etc/cron.deny
qiankai
用户的计划任务
每个用户都有自己的计划任务
[root@sc-server cron]# crontab -u xiaoyang -e
crontab: no changes made to crontab
[root@sc-server cron]# crontab -u sumeng -e
crontab: no changes made to crontab
[root@sc-server cron]# crontab -u root -e
crontab: no changes made to crontab
存放计划任务的文件夹
主要设置文件
用户定义的设置,位于文件:/var/spool/cron/用户名
[root@sanchuang cron]# cd /var/spool/cron/
[root@sanchuang cron]# ls
chenlin jinzhiyang qiankai root
[root@sanchuang cron]# cat qiankai
*/1 * * * * mkdir -p ~/$(date +%F_%H%M%S).qiankai
[root@sanchuang cron]# cat chenlin
*/1 * * * * mkdir -p ~/$(date +%F_%H%M%S).chenlin
[root@sanchuang cron]# cat jinzhiyang
*/1 * * * * mkdir -p ~/$(date +%F_%H%M%S).jin
[root@sanchuang cron]# cat root
#0 23 * * * bash /lianxi/backup_log.sh
*/1 * * * * bash /lianxi/sanchuang/create_dir.sh
0 */1 * * * mkdir -p /lianxi/$(date +%F_%H%M%S).fengdeyong
* */1 * * * mkdir -p /lianxi/$(date +%F_%H%M%S).fengdy
删除计划任务
终止它就是把那个计划任务删掉
可以把计划任务注释,就不会执行了--》推荐注释
当然也可以删除
可以删除对应的文件
[root@sc-server log]# cd /var/spool/cron/ 存放计划任务的目录 一个用户对应一个文件,存放自己的计划任务
[root@sc-server cron]# ls
root xiaoyang
[root@sc-server cron]# cat root
01 05 31 3 * bash /backup/backup_log.sh
#01 */2 * * * ntpdate ntp.sjtu.edu.cn
*/1 * * * * ntpdate ntp.sjtu.edu.cn
[root@sc-server cron]# cat xiaoyang
*/1 * * * * mkdir ~/${RANDOM}-xiaoyang
[root@sc-server cron]# crontab -e
crontab: no changes made to crontab