bash多进程

function ping_target()
{
        local target=$1
        timeout 0.5 ping $target -c 1 > /dev/null
        if [ "$?" == "0" ]
        then
                echo "ping succ: $target" >> "$RESULT_FILE"
        else
                echo "ping failed: $target" >> "$RESULT_FILE"
        fi
}

count_all=0
count_fail=0
count_succ=0

MAX_CONCURRENT=50
current_jobs=0
truncate -s0 "$RESULT_FILE"
while read line
do
        count_all=`expr $count_all + 1`
        ping_target $line &

        while ((current_jobs >= MAX_CONCURRENT))
        do
                current_jobs=$(pstree -p $$ |wc -l)
                sleep 0.1
        done
done <<< "$list"

wait

count_succ=$(grep -c "succ" "$RESULT_FILE")
count_fail=$(grep -c "failed" "$RESULT_FILE")
total=$(wc -l < "$RESULT_FILE")

 

posted on 2025-07-11 17:39  toong  阅读(8)  评论(0)    收藏  举报