shell 在bash脚本中执行curl调用以检查API结果

kqhtkvqz  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(118)

我试图测量一个特定的数据块到达p2p网络中所有节点的时间。
这是一个bash脚本。我有几个服务器(盒子),在每个服务器上,都有一些节点在运行。
我的目标是测量所有节点都通过其API响应并获得数据的时间。
这是一个bash脚本。我不知道在bash脚本中可以并行调用到什么程度。因为现在它首先遍历每个盒子,然后向盒子的每个节点发出curl调用,然后移动到下一个盒子。我觉得这可能会影响我的结果。
有没有更好的方法来做到这一点?

found=0
   while :
   do  
     echo "checking data is at node..."
     for i in "${BOXES[@]}"
     do
       for (( j=0; j < $NODE_PER_BOX; j++ )) 
       do
         res=$(curl http://ip:port/api)
         echo $res 
         # HERE I NEED TO CHECK FOR THE CONDITION, NOT DONE YET 
         # I know the exit condition is never met here yet
       done
     done                                               
  done
qvtsj1bj

qvtsj1bj1#

您可以通过在后台使用&放置命令或代码块来创建进程,然后使用wait让它们完成。
范例:

time { # measure the time it takes to finish all lookups in parallel
    for box in "${BOXES[@]}"; do
        for (( j=0; j < NODE_PER_BOX; j++ )); do
        {
            res=$(curl -s http://ip:port/api)
            # HERE I NEED TO CHECK FOR THE CONDITION, NOT DONE YET

            # Write the result of the condition check to a unique file per query:
            echo "$res" > "${box}_${j}.res"
        } &  # & puts the job in a background process
        done
    done

    wait # wait until all jobs are done
}        # end of time measuring block

# read all the result files here

相关问题