在不使用crontab的特定日期运行脚本

时间:2018-10-24 05:08:45

标签: unix

我有一个驾驶脚本,该脚本在内部调用多个脚本。

`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

2 个答案:

答案 0 :(得分:1)

尝试一下:

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

让我知道是否有帮助。

答案 1 :(得分:0)

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