Unix 在不使用crontab的特定日期运行脚本
我有一个驱动脚本,它在内部调用多个脚本Unix 在不使用crontab的特定日期运行脚本,unix,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
`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
用于单次执行。谢谢,但是如果任何脚本出现故障,有没有办法使用重试机制。对于例如script_1.sh失败,那么如果我重新运行它,应该运行script_1.shonly@user2672739这里可以实现重试逻辑,但这将是相当复杂的。很可能,使用类似于Airflow
的调度程序,它的功能非常丰富,具有重试功能,只需重新运行当前的子脚本。通过shell脚本实现这一点对我来说不是一个好主意。我认为这可以通过shell脚本实现。你能帮我解释一下它的可能性吗我已经在我的代码中为script\u 1.sh
更新了重试。你可以把它复制给其他人。检查它。实际上不是重试机制如果script_1.sh失败,那么如果我重新运行main_script.sh,它应该只运行script_1.sh。例如,script_1.sh在25日失败,在26日重新运行,在这种情况下,script_1.sh将不运行,其他script_2.sh也将运行,这将完成日常作业