There are 2 scripts that run daily on the mission plan
DTS_RSYNC.SH is responsible for downloading the SQL files that are backed up online
DTS_INPUT.SH is responsible for importing the downloaded backup files to the local MySQL
Now, the problem is that the time to complete the download of the backup file is not fixed. It was 6 o'clock in the evening and it is now 7 points before it can be completed and may be increased later.
The download log is as follows:
2015-10-15 0:10:01 Start Transfer
2015-10-15 7:19:35 Transfer Complete
The mission plan is as follows:
#同步express的备份
0 * * */opt/dts_rsync.sh &
#导入备份文件
7 * * */opt/dts_input.sh &
What you need to do is to extract the download completion time and then add 1 hours to the hour portion of the import backup file time.
Because the shell cannot edit the task schedule directly.
You can write the task plan content to a TXT file, and then use the command to overwrite the contents of the TXT to the task schedule
The command is as follows:
/usr/bin/crontab/opt/cron.txt
The complete shell script cron is as follows:
#!/bin/bash
echo "#######################" >>/opt/cron.log
#记录执行时间
Statime= ' Date +%y-%m-%d ' "%h:%m:%s '
echo "$statime" >>/opt/cron.log
#任务计划开始时间, extraction hours
A= ' Cat/opt/cron.txt | grep dts_input.sh | awk ' {print $} '
#传输完成时间, extraction hours
b= ' Tail-1/tmp/dts_message.log | awk ' {print $} ' | Cut-d ":"-F 1 '
echo "Task Scheduler start time $ A" >>/opt/cron.log
echo "Transfer complete Time $b" >>/opt/cron.log
#判断传输完成时间是否大于等于任务计划时间
If [$b-ge $a];then
#增加1个小时
C= '/usr/bin/expr $a + 1 '
#修改任务计划文件
/bin/sed-i "s/$a/$c/g"/opt/cron.txt
#覆盖任务计划
/usr/bin/crontab/opt/cron.txt
echo "Task scheduled start time status error, change hours for $c" >>/opt/cron.log
Else
echo "Task scheduled start time status OK, hour is $ A" >>/opt/cron.log
Fi
Cron.txt content is as follows:
#同步express的备份
0 * * */opt/dts_rsync.sh &
#导入备份文件
7 * * */opt/dts_input.sh &
#检查下载时间
*/1 * * */opt/cron.sh
#每周日清空日志记录
* * 0/opt/clean_log.sh
This article is from the "Falling Star" blog, make sure to keep this source http://xiao987334176.blog.51cto.com/2202382/1703292
Linux Shell Script Tuning Task Scheduler