第二章:Linux定时器
Crond定时任务服务应用
1、cron 定时任务的名字
2、cron 定时任务进程名
3、crontab 管理定时任务命令
检查crond服务相关的软件包
[root@localhost opt]# rpm -qa | grep cron cronie-1.4.11-19.el7.x86_64 cronie-anacron-1.4.11-19.el7.x86_64 yum-cron-3.4.3-167.el7.centos.noarch crontabs-1.11-6.20121102git.el7.noarch
检查cron服务是否运行
[root@localhost opt]# systemctl status crond #centos7 ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 一 2020-11-23 10:34:46 CST; 6 years 4 months left Main PID: 733 (crond) CGroup: /system.slice/crond.service └─733 /usr/sbin/crond -n
[root@localhost opt]# service crond status #centos6
cron任务分为两类
系统的定时任务
cron服务除了会在工作时查看、car、spool、cron文件夹下的定时任务文件以外,还会查看/etc/cron/.d目录以及/etc/anacrontab下面的文件内容,里面存放每天、每周、每月需要执行的系统任务
[root@localhost opt]# ll /etc/| grep cron* -rw-------. 1 root root 541 4月 11 2018 anacrontab drwxr-xr-x. 2 root root 21 11月 23 2020 cron.d #系统的定时任务 drwxr-xr-x. 2 root root 65 11月 24 2020 cron.daily #每天的定时任务 -rw-------. 1 root root 0 4月 11 2018 cron.deny drwxr-xr-x. 2 root root 46 11月 24 2020 cron.hourly #每小时执行的任务 drwxr-xr-x. 2 root root 6 6月 10 06:14 cron.monthly #每月的定时任务 -rw-r--r--. 1 root root 452 12月 8 2020 crontab drwxr-xr-x. 2 root root 6 6月 10 06:14 cron.weekly #每周的定时任务
系统定时任务配置文件/etc/crontab
[root@localhost opt]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin #路径信息很少,因此定时任务用绝对路径 MAILTO=root #执行的结果邮件发送给用户 # 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
#每一行,就是一条周期性任务
user-name 是以某一个用户身份运行任务
command to be executed 任务是什么
用户定时任务计划
当系统管理员(root)或者普通用户(xiaolin)创建了需要定期执行的任务,可以使用crontab命令配置,crond服务在启动时,会每分钟查看/car/spool/cron路径下以系统用户名命名的定时任务文件,以确定是否需要执行的任务
[root@localhost opt]# ll /var/spool/cron 总用量 4 -rw-------. 1 root root 62 12月 8 2020 root #查看此root定时任务文件的内容 [root@localhost opt]# cat /var/spool/cron/root */5 * * * * /user/sbin.ntpdate nep1.aliyun.com & > /dev/null #等用于crontab -l 命令
crontab命令
crontab命令备用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似
参数 | 解释 | 使用示例 |
-l | list查看定时任务 | crontab -l |
-e | edit编辑定时任务,建议手动编辑 | crontab -e |
-i | 删除定时任务,,提示用户确认删除,避免删错 | crontab -i |
-r | 删除定时任务,移除/var/spool/cron/username文件,全没了 | crontab -r |
-u user | 指定用户执行任务,root可以管理普通用户的任务计划 | crontab-u xiaolin-l |
crontab命令就是修改/var/spool/cron中的定时任务文件
用户查看定时任务
crontab -d #列出用户设置的定时任务,等于cat /var/spool/cron/root crontab -e #编辑用户的定时任务,等于如上命令 vi /var/spool/cron/root文件
检查crond服务是否运行
[root@localhost opt]# systemctl is-active crond active [root@localhost opt]# ps -ef|grep crond root 733 1 0 6月27 ? 00:00:08 /usr/sbin/crond -n root 22463 31737 0 12:22 pts/0 00:00:00 grep --color=auto crond
定时任务相关的文件
/var/spool/cron 定时任务的配置文件所在目录 /var/log/cron 定时任务日志文件 /etc/cron.deny 定时任务黑名单
定时任务日志格式介绍
定时任务语法格式
口诀:什么时候做什么是
查看定时任务配置文件
[root@localhost opt]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# 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 #周几 (Sunday=0 or 7)周日=0或7
# | | | | |
# * * * * * user-name command to be executed
案例
每天上午8点30,去上学 30 08 * * * go to school 每天晚上12点回家回家睡觉 00 00 * * * go home
定时任务符号
crontab任务配置基本格式: * * * * * command 分钟(0-59) 小时(0-23) 日期(1-31) 月份(1-12) 星期(0-6,0代表星期天) 命令 第1列表示分钟1~59 每分钟用*或者 */1表示 第2列表示小时1~23(0表示0点) 第3列表示日期1~31 第4列表示月份1~12 第5列标识号星期0~6(0表示星期天) 第6列要运行的命令 (注意:day of month和day of week一般不同时使用) (注意:day of month和day of week一般不同时使用) (注意:day of month和day of week一般不同时使用)
时间表示法:
特定值,时间点有效取值范围内的值
通配符,某时间点有效范围内的所有值,表示“每”的意思
特殊符号 | 含义 |
* | *号表示“每”的意思,如00 23 cmd表示每月每周每日的23:00整点执行命令 |
- | -号表示时间的范围分隔符,如8-10,代表每天的8、9、10执行命令 |
, |
,号,表示分割时段,30 5,7表示每天的5点和7点执行命令 |
/n | n表示可以整除的数字,每隔n的单位时间,如隔10分钟表示*/10 |
示例:
0 * * * * 每小时执行,每小时的整点执行 1 2 * * 4 每周执行,每周四的凌晨2点1分执行 1 2 3 * * 每月执行,每月的3号的凌晨2点1分执行 1 2 3 4 * 每年执行,每年的4月份3号的凌晨2点1分执行 1 2 * * 3,5 每周3和周五的2点1分执行 * 13,14 * * 6,0 每周六、周日的下午1点和2点的每一分钟都执行 0 9-18 * * 1-5 周一到周五的每天早上9点一直到下午6点的每一个整点(工作日的每个小时整点) */10 * * * * 每隔10分钟执行一次任务 *7 * * * * 如果没法整除,定时任务则没意义,可以自定制脚本控制频率 定时任务最小单位是分钟,想完成秒级任务,只能通过其他方式(编程语言)
*/1 * * * * /bin/sh /scripts/data.sh #每分钟执行命令
30 3,12 * * * /bin/sh /scripts/data.sh #每天的凌晨3点半,和12点半执行脚本
30 */6 * * * /bin/sh /scripts/data.sh #每隔6小时,相当于6、12、18、24点的半点时刻,执行脚本
30 8-18/2 * * * /bin/sh /scripts/data.sh # 30代表半点,8-18/2表示早上8点到下午18点之间每隔两小时也就是8、10、12、14、16、18的半点时刻执行脚本
30 21 * * * /opt/nginx/sbin/nginx -s reload #每天晚上9点30重启nginx
45 4 1,10 * * /bin/sh /scripts/data.sh #每月的1、10号凌晨4点45执行脚本
10 1 * 6,0 /bin/sh /scripts/data.sh #每周六、周日的凌晨1点10分执行命令
0,30 18-23 * * * #每天的18点到23点之间,每隔30分钟执行一次
00 */1 * * * /bin/sh /scripts/data.sh #每隔一小时执行一次
00 11 * 4 1-3 /bin/sh /scripts/data.sh #4月份的周一到周三的上午11点执行脚本
# 每天早上7点到上午11点,每2小时运行cmd命令
00 07-11/2 * * * CMD
0 6 * * * /var/www/test.sh #每天6点执行脚本
0 4 * * 6 /var/www/test.sh #每周六凌晨4:00执行
5 4 * * 6 /var/www/test.sh #每周六凌晨4:05执行
40 8 * * * /var/www/test.sh #每天8:40执行
31 10-23/2 * * * /var/www/test.sh #在每天的10:31开始,每隔2小时重复一次
0 2 * * 1-5 /var/www/test.sh #每周一到周五2:00
0 8,9 * * 1-5 /var/www/test.sh #每周一到周五8:00,每周一到周五9:00
0 10,16 * * * /var/www/test.sh #每天10:00、16:00执行
生产环境用户配置定时任务流程
需求:每分钟向/opt/log文件中写入一句话“hello wenchang”
第一步:创建文件/opt/log
[root@localhost opt]# ll 总用量 4 drwxr-xr-x. 2 root root 6 12月 8 2020 - drwxr-xr-x. 2 root root 6 12月 8 2020 9{a-z} -rw-r--r--. 1 root root 81 12月 8 2020 bak.sh -rw-r--r--. 1 root root 0 7月 13 12:51 log drwxr-xr-x. 2 root root 6 12月 8 2020 p
第二步,编辑定时任务文件,写入需要定时执行的任务
[root@localhost opt]# crontab -e * * * * * /usr/bin/echo"hello wenchang" >> /opt/log
第三步,检查定时任务
[root@localhost opt]# crontab -l * * * * * /usr/bin/echo "hello wenchang" >> /opt/log
第四步:可以检测文件内容
[root@localhost opt]# tail -f /opt/log
hello wenchang
每5分钟让服务器进行同步时间
[root@localhost opt]# crontab -e */5 * * * * /user/sbin.ntpdate nep1.aliyun.com & > /dev/null
每晚0点整,把站点目录/var/www/html下的内容打包备份到/date目录下
提醒,tar命令不建议使用绝对路径打包,特殊情况可以使用-P参数
1.检查文件夹是否存在,不存在则创建 [root@pylinux ~]# ls -d /var/www/html /data ls: 无法访问/var/www/html: 没有那个文件或目录 ls: 无法访问/data: 没有那个文件或目录 2.创建文件夹 [root@pylinux ~]# mkdir -p /var/www/html /data [root@pylinux ~]# ls -d /var/www/html /data /data /var/www/html 3.创建测试文件 [root@pylinux ~]# touch /var/www/html/chaoge{1..10}.txt [root@pylinux ~]# ls /var/www/html/ chaoge10.txt chaoge1.txt chaoge2.txt chaoge3.txt chaoge4.txt chaoge5.txt chaoge6.txt chaoge7.txt chaoge8.txt chaoge9.txt 4.打包压缩命令 [root@pylinux www]# tar -zcvf /data/bak_$(date +%F).tar.gz ./html/
编写shell脚本
[root@pylinux scripts]# cat bak.sh #!/bin/bash cd /var/www && \ /bin/tar -zcf /data/bak_$(date +%F).tar.gz ./html
crontab -e 写入 00 00 * * * /bin/sh /server/scripts/bak.sh > /dev/null 2>&1 #/server/scripts/bak.sh 脚本存放路径 #解释 >/dev/null 2>&1 代表把所有输出信息重定向到黑洞文件 > 是重定向符号 /dev/null是黑洞文件 2>&1 代表让标准错误和标准输出一样 此命令表示将脚本执行的正常或者错误日志都重定向到/dev/null,也就是什么都不输出 >/dev/null 2>&1 等价于 1>/dev/null 2>/dev/null 等价于 &> /dev/null
取消定时任务发邮件功能
1.定时任务的命令 > /dev/null #命令的执行正确结果输出到黑洞,标准错误还是报错 2.定时任务的命令 &> /dev/null #组合符 &> 正确和错误的输出,都写入黑洞,危险命令,有风险,慎用
补充anacron
如果由于机器故障关机,定时任务未执行,下次开机也不会执行任务
使用anacron下次开机会扫描定时任务,将未执行的,全部执行,服务区很少关机重启,所以忽略。