Linux shell script to adjust the task plan
There are 2 scripts to run the task plan every day
Dts_rsync.sh downloads online backup SQL files
Dts_input.sh is responsible for importing the downloaded backup file to the local Mysql
One problem is that the backup file download time is not fixed. In the past, the download was completed at a.m., and now it can be completed at a.m.. It may be added later.
The download log is as follows:
0:10:01 Start Transmission
7:19:35 Transmission completed
The task plan is as follows:
# Synchronous backup of express
10 0 ***/opt/dts_rsync.sh &
# Importing backup files
01 7 ***/opt/dts_input.sh &
All you need to do is extract the download completion time, and then increase the hours in the backup file import time by one hour.
The shell cannot directly edit the task plan.
You can write the task plan content to a txt file, and then use the command to overwrite the txt content to the task plan
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
# Record execution time
Statime = 'date + % Y-% m-% d "" % H: % M: % s'
Echo "$ statime">/opt/cron. log
# Scheduled task start time, extracted by hour
A = 'cat/opt/cron.txt | grep dts_input.sh | awk '{print $2 }''
# Transfer completion time, extraction hour
B = 'Tail-1/tmp/dts_message.log | awk '{print $2}' | cut-d ":"-f 1'
Echo "task scheduled start time $ a">/opt/cron. log
Echo "transfer completion time $ B">/opt/cron. log
# Determine whether the transfer completion time is greater than or equal to the scheduled time of the task
If [$ B-ge $ a]; then
# Increase by 1 hour
C = '/usr/bin/expr $ a + 1'
# Modifying the task scheduler File
/Bin/sed-I "s/$ a/$ c/g"/opt/cron.txt
# Overwrite the task plan
/Usr/bin/crontab/opt/cron.txt
Echo "task Scheduled Start Time status ERROR, changed to $ c">/opt/cron. log
Else
Echo "The task plan start time status is OK, and the hour is $ a">/opt/cron. log
Fi
The contents of cron.txt are as follows:
# Synchronous backup of express
10 0 ***/opt/dts_rsync.sh &
# Importing backup files
01 7 ***/opt/dts_input.sh &
# Check the download time
01 */1 ***/opt/cron. sh
# Clearing log records every Sunday
01 23 ** 0/opt/clean_log.sh
This article permanently updates the link address: