需求如下:每天kettle Job调度可能报错一大堆,为了重跑报错Job,又不能一次全部执行报错Job(服务器负载过大,导致每个Job执行时间变长,甚至报错不能分配内存错误),必须等一些Job执行完毕后再执行,浪费睡觉时间,咱们可以写脚本,早上起来发现报错后,执行脚本,然后就可以去继续睡觉了。
脚本如下:
#!/bin/ksh
#job执行的2个日期参数
CurDay=`date +%Y%m%d`
LastDay=`date -d yesterday +%Y%m%d`
#job限制上限
JobLimit=5
#错误job名称存放文件
JobFile="/home/kettle/Job/errorjob"
#日志文件
LogDir="/home/kettle/log/Rerun"
LogFile="${LogDir}/LoopRerunErrorJob_$(date +%Y%m%d%H%M%S).log"
#记日志
Log(){
CurDate=`date +"%Y-%m-%d %H:%M:%S"`
echo "INFO ${CurDate} $1" >> $LogFile
}
while read line || [[ -n $line ]]
do
typeset -L v1=$line
typeset -R v2=$v1
line=$v2
Aline="${Aline} ${line}"
#echo $Aline
done<"$JobFile"
Log $Aline
Array=($Aline)
ArrLen=${#Array[@]}
Log "Error Jobs Numbers:${ArrLen}"
i=0
while (( $i <${ArrLen} ))
do
JobCnt=`ps aux|grep "IncStart=${LastDay} -param:IncEnd=${CurDay}"|grep -v grep|grep -v Load|wc -l`
if [ JobCnt -lt ${JobLimit} ]
then
Log "sh /home/kettle/bin/RunKettleJob.sh ${Array[i]} ${LastDay} ${CurDay}"
sh /home/kettle/bin/RunKettleJob.sh ${Array[i]} ${LastDay} ${CurDay} &
sleep 10
(( i=i+1 ))
else
Log "Wait 1.5 min..."
sleep 90
fi
Log "i=$i"
done