在没有 crontab 的特定日期运行脚本



我有一个在内部调用多个脚本的驱动脚本。

`sh main_script.sh > main_script.log`

内部main_script.sh

sh -x script_1.sh
sh -x script_2.sh
sh -x script_3.sh

我必须在特定日期运行内部脚本并相应地管理故障场景,如果任何脚本失败,那么在重试时它应该只运行该特定的子脚本。

script_1.sh - 25th of every month
script_2.sh - daily 
script_3.sh - Every quarterly month end

试试这个:

current_day=$(date +%e)
current_time=$(date +"%H:%M")
current_month=$(date +"%m")
## Execute script_1.sh on 25th every month and at time = 10 AM
if [ $current_day == 25 ] && [ $current_time == '10:00' ]
then
n=0
until [ $n -ge 5 ]
do
sh -x script_1.sh && break  
n=$[$n+1]
sleep 15
done
fi
##This will keep retrying in every 15 seconds for 5 times and break out of loop if the command got successful    

## Execute script_2.sh daily at time = 10 AM
if [ $current_time == '10:00' ]
then
sh -x script_2.sh
fi
## Execute script_3.sh for every quarter month-end(run for months Apr,Aug,Dec on 30th at 10 AM)
if [ $current_month == 4 ] || [ $current_month == 8 ] || [ $current_month == 12 ]
then 
if [ $current_day == 30 ] && [ $current_time == '10:00' ]
then
sh -x script_3.sh
fi
fi

main_script.sh替换为上面的代码。并始终执行main_script.sh

让我知道它是否有帮助。

旁边crontab,还有at功能,可以安排任务。主要区别在于crontab用于重复任务,而at用于单个执行。

最新更新