30.1) What is timed task
30.2) Why use crond
The main task is to do some planning periodic tasks such as data backup timer 3:00, 23 o'clock at night snapped open website interfaces, 0:00 closed seize interface.
Scheduled tasks are divided into the following two use cases:
1. The system timing task level
Clean up temporary files, system information collection, cutting the log file
2. User-level regular tasks
A timing synchronization time to the internet, regular backup system configuration files, database data scheduled backups.
30.3) crontab configuration file
[root@centos7 ~]# vim /etc/crontab
= The SHELL / bin / the bash
the PATH = / sbin: / bin: / usr / sbin: / usr / bin
a MAILTO = the root # crontabs the For Details See man. 4 # Example of Job Definition: # .------- minute --------- (0 - 59) # | .------------- hour (0 - 23) # | | .--------- - Day of month The (1 - 31) # | | | .------- month The (1 - 12) OR jan, feb, Mar, apr ... # | | | | .---- Day of Week (0 -. 6) (or the Sunday = 0. 7) oR Sun, Mon, Tue, Wed, Thu, Fri, SAT # | | | | | # * * * * * User-name Command Executed to BE Notes: 1 ) * represents an arbitrary (minutes, hours, days, months, weeks) perform time 2) - represents a time period, such as 5-7 points 3), separated by a period represented as 6,0,4 represented Saturday, day, four 4) / n 1 represents every unit of time, such as * / 10 per 10 minutes
30.4) crontab time to prepare specifications
00 02 * * * ls #每天的凌晨2点整执行
00 02 1 * * ls #每月的1日的凌晨2点整执行
00 02 14 2 * ls #每年的2月14日凌晨2点执行
00 02 * * 7 ls #每周天的凌晨2点整执行
00 02 * 6 5 ls #每年的6月周五凌晨2点执行
00 02 14 * 7 ls #每月14日或每周日的凌晨2点都执行
00 02 14 2 7 ls #每年的2月14日或每年2月的周天的凌晨2点执行
*/10 02 * * * ls #每天凌晨2点,每隔10分钟执行一次
* * * * * ls #每分钟都执行
00 00 14 2 * ls #每年2月14日的凌晨执行命令
*/5 * * * * ls #每隔5分钟执行一次
00 02 * 1,5,8 * ls #每年的1月5月8月凌晨2点执行
00 02 1-8 * * ls #每月1号到8号凌晨2点执行
0 21 * * * ls #每天晚上21:00执行
45 4 1,10,22 * * ls #每月的1,10,22号的4:45执行
45 4 1-10 * * ls #每月的1到10号的4:45执行
3,15 8-11 */2 * * ls #每隔两天的上午8点到11点的第3和第15分钟执行
0 23-7/1 * * * ls #晚上11点到早上7点之间,每隔一个小时执行
15 21 * * 1-5 ls #周一到周五每天晚上21:15执行
30.5)crontab命令选项
-e #编辑定时任务
-l #查看定时任务
-r #删除定时任务
-u #指定其他用户
30.6)计划任务实践示例
示例1:使用root用户每2分钟执行一次时间同步
#需提前安装软件包
[root@centos7 ~]# yum install -y ntpdate
#先在Linux系统上查看命令是否正确
[root@centos7 ~]# ntpdate ntp.aliyun.com
26 Jul 15:20:49 ntpdate[19706]: step time server 203.107.6.88 offset -1.696419 sec
#配置定时任务
#编辑定时任务
[root@centos7 ~]# crontab -e -u root
#每2分钟执行一次时间同步 by wxg At 2019.07.26
*/2 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
#查看定时任务
[root@centos7 ~]# crontab -l -u root
#每2分钟执行一次时间同步 by wxg At 2019.07.26
*/2 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
30.7)计划任务如何调试
30.7.1)crond调试
1)调整任务每分钟执行的频率, 以便做后续的调试。
2)如果使用cron运行脚本,请将脚本执行的结果写入指定日志文件, 观察日志内容是否正常。
3)命令使用绝对路径, 防止无法找到命令导致定时任务执行产生故障。
4)通过查看/var/log/cron日志,以便检查我们执行的结果,方便进行调试。
30.7.2.crond编写思路
1.手动执行命令,然后保留执行成功的结果。
2.编写脚本
脚本需要统一路径/scripts
脚本内容复制执行成功的命令(减少每个环节出错几率)
脚本内容尽可能的优化, 使用一些变量或使用简单的判断语句
脚本执行的输出信息可以重定向至其他位置保留或写入/dev/null
3.执行脚本
使用bash命令执行, 防止脚本没有增加执行权限(/usr/bin/bash)
执行脚本成功后,复制该执行的命令,以便写入cron
4.编写计划任务
加上必要的注释信息, 人、时间、任务
设定计划任务执行的周期
粘贴执行脚本的命令(不要手敲)
5.调试计划任务
增加任务频率测试
检查环境变量问题
检查crond服务日志 /var/log/cron
30.8)综合示例:使用定时任务发邮件
步骤一:配置邮件发邮件
1)现在/etc/mail.rc配置文件中配置邮件及下载证书;
2)登录邮箱客户端或者PC端,开启POP3/SMTP服务,获得授权码;
[root@centos7 ~]# vim /etc/mail.rc
#发件人
set [email protected]
#邮件服务器
set smtp=smtp.qq.com
#发件人用户名
set [email protected]
#发件人密码(QQ邮箱不可以使用密码,只能使用授权码)
set smtp-auth-password=etadwxmucvmwbded
#登录方式
set smtp-auth=login
#邮件服务器协议及端口
set smtp=smtps://smtp.qq.com:465
#忽略证书
set ssl-verify=ignore
#指定证书位置
set nss-config-dir=/root/.certs
Type :quit<Enter> to exit Vim 76,1 Bot
登录邮箱客户端或者PC端,点击“设置”,点击“账户”,
开启POP3/SMTP服务
生成授权码
3)编辑文本,准备邮件附件
[root@centos7 /]# cd /
[root@centos7 /]# vim book1.txt
喜欢你的人很多,不缺我一个,但我爱的人很少,只有你一个!
[root@centos7 /]# vim book2.txt
一直想说,无论走到哪里,最想去的是你的身边。
4)发送邮件测试,编写Linux命令,邮箱收到测试邮件,表示命令可以正常使用。
cat book1.txt | mail -s '表白' [email protected]
5)接下来写脚本,放入定时任务中
#创建一个情书的目录
[root@centos7 ~]# mkdir /txt_dir
#将所有情书移动到该目录下
[root@centos7 /]# mv /book* /txt_dir/
#编写脚本
[root@centos7 /]# vim send_mail.sh
#!/bin/bash
qs_name=`ls -1 /root/qingshu_dir/|head -1`
cat /root/qingshu_dir/${qs_name} |mail -s '致我最爱的小姐姐' [email protected]
if [ $? -eq 0 ];then
rm -f /root/qingshu_dir/$qs_name
fi
#编辑crontab
[root@centos7 /]# crontab -e
#每天发情书 by wxg At 2019.7.26
* * * * * /bin/bash /root/send_mail.sh &>/dev/null