Shell background loop assignment and call the same script

Project scenario:

Call the script in the background, and realize the circular assignment to this script. Finally, the query results are written to the same log. (Nohup is embedded in the for loop and executed in the background)
According to the project requirements, we want to assign different numbers to the query script to check the number in the table and write the result to the log.


Problem Description

Executing the following calling script will call the query script check_plan.sh in the background, and it turns out that the query results of some numbers are incomplete, and there are cases of missing data.

arr1=('191*****343' '130*****963')
arr2=('2022101114' '2022101823')
arr3=('20221011' '20221018')

length=${
    
    #arr1[*]}

for ((k=0;k<length;k++))
do 
	nbr=${
    
    arr1[$k]}
	time_tag=${
    
    arr2[$k]}
	cur_day=${
    
    arr3[$k]}
	echo $nbr,$time_tag,$cur_day

nohup sh /apps/bin/model/data_val/test/check_plan.sh  $nbr $time_tag $cur_day > /apps/bin/model/data_val/test/check_plan_$(date +%Y%m%d%H%M).log 2>&1 &

done

Cause Analysis:

Execute the above script, the running status is not waiting for the completion of the first number query, and then enter the sequence of the second number query. Although different assignments are executed cyclically and serially, the same script is executed in the same time period and the query results are written into the same log, so data may be written to the same log at the same time, resulting in Incomplete data error.


solution:

Problem solved: Write query results to a script at the same time.

Solution ideas:
Solution 1 : In the process of cyclic assignment, wait for the previous assignment process to end before entering another assignment process. (Process serial)
Option 2 : Different numbers of query results are written to different logs (process parallel)
Option 1 has the advantage that too many log files will not be generated, and the disadvantage is that the process serial leads to a relatively long running time.
The advantage of solution 2 is that the parallel operation efficiency is faster, and the disadvantage is that multiple log files will be generated.

Code:

Solution 1:
Adding wait in the for loop will block the execution of the current number query process, and continue to execute the next number process until all sub-processes of the current number query process are executed.

arr1=('191*****343' '130*****963')
arr2=('2022101114' '2022101823')
arr3=('20221011' '20221018')

length=${
    
    #arr1[*]}

for ((k=0;k<length;k++))
do 
	nbr=${
    
    arr1[$k]}
	time_tag=${
    
    arr2[$k]}
	cur_day=${
    
    arr3[$k]}
	echo $nbr,$time_tag,$cur_day

nohup sh /apps/bin/model/data_val/test/check_plan.sh  $nbr $time_tag $cur_day > /apps/bin/model/data_val/test/check_plan_$(date +%Y%m%d%H%M).log 2>&1 &

wait #加入wait

done

Solution 2:
Change the write file file_name in the query script check_plan.sh, so that a number corresponds to a log log file.

nbr=$1
time_tag=$2
cur_day=$3

echo $nbr,$time_tag,$cur_day

global_commmand="beeline --showHeader=false --outputformat=dsv -e"
file_path="/apps/bin/model/data_val/"
file_name="fd_val_result_${nbr}_${time_tag}.log"#一个号码对应一份log日志
file_name_1="fd_val_result_info_$nbr.log"

if [[ ${
    
    #condition_rule_234} -gt 0 ]];then
        echo "满足规则2,3,4命中并且不满足剔除条件:剔除06:00-9:00有主叫通话记录且被叫为手机号码"  >> $file_path/$file_name

        #echo "判断是否满足条件(1)入网时间大于7天且小于等于30天"  >> $file_path/$file_name step_1_1
        condition_online_info=`$global_commmand "use databases;select * from model_range_fd_rule_3_${time_tag} where calling_nbr in ('$nbr') ;"`

        if  [[ ${
    
    #condition_online_info} -eq 0 ]];then 
                #step_1_1_1
                echo "532模型:$nbr不满足条件:入网时间大于7天且小于等于30天,因此没有命中" >> $file_path/$file_name

                #step_1_1_1_1
                #第一步:查看业务信息中间表的具体的入网时长(证据说明问题)。
                result_online_info=`$global_commmand "use databases;select * from all_mobile_user_info_${cur_day} where calling_nbr in ('$nbr') ;"`  >> $file_path/$file_name
                #step_1_1_1_2
                #第二步:判断是否有被"业务信息不正常的结果表"命中
                result_untj_info=`$global_commmand "use databases;select * from model_fd_online_result_info30 where calling_nbr in ('$nbr');"` 

                if  [[ ${
    
    #result_untj_info} -gt 0 ]];then 
                        echo "输出满足条件的号码命中批次(业务信息不正常)"  >> $file_path/$file_name
                        echo "$result_untj_info"  >> $file_path/$file_name
                else
                        echo "没有被(业务信息不正常)命中??? 大问题了,代码有问题了(哭.jpg)"
                fi

        else 
                #step_1_1_2
                echo "满足条件(1)入网时间大于7天且小于等于30天"  >> $file_path/$file_name
                echo "满足条件的号码命中批次(业务信息正常)如下"  >> $file_path/$file_name
                result_tj_info=`$global_commmand "use databases;select * from model_fd_stat_real_info30 where calling_nbr in ('$nbr') and etl_cycle_id='${cur_day}';"`  
                echo "$result_tj_info" >> $file_path/$file_name


        fi 
fi

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/127725089