主頁(yè) > 知識(shí)庫(kù) > Shell中實(shí)現(xiàn)“多線程”執(zhí)行腳本文件完美解決方案

Shell中實(shí)現(xiàn)“多線程”執(zhí)行腳本文件完美解決方案

熱門標(biāo)簽:佛山高德地圖標(biāo)注中心 excel地址地圖標(biāo)注 旅游地圖標(biāo)注大全 百度地圖標(biāo)注圖標(biāo)更換 陜西電銷卡外呼系統(tǒng)怎么安裝 杭州機(jī)器人外呼系統(tǒng) 百度地圖的地圖標(biāo)注 東莞電銷機(jī)器人價(jià)格一覽表 地圖標(biāo)注超出范圍怎么辦

即比如我有100個(gè)可執(zhí)行文件,互相間沒(méi)有特別的先后執(zhí)行關(guān)系,如CODE:

復(fù)制代碼 代碼如下:

job_1
job_2
job_2
.....
job_100

想用csh/bash來(lái)多線程調(diào)用執(zhí)行。

比如一次開(kāi)5個(gè)線程,那么job_1,2,3,4,5一起先開(kāi)始,那么其中任何一個(gè)線程如果先執(zhí)行完成,則繼續(xù)執(zhí)行下一個(gè)沒(méi)有初執(zhí)行過(guò)的文件,如job_6,7,8....,這樣一直以所指定的線程數(shù)來(lái)執(zhí)行所有100個(gè)文件。

我本來(lái)想用 "" 來(lái)放入后臺(tái),可是這樣我一次可以指定5放入后臺(tái),但是無(wú)法知道其中任何一個(gè)程序何時(shí)執(zhí)行完畢,所以也無(wú)法繼續(xù)執(zhí)行下一個(gè)程序啊!

完美解決方案:

復(fù)制代碼 代碼如下:

-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; cat job_1
#!/bin/bash
n=$((RANDOM % 5 + 1))
echo "$0 sleeping for $n seconds ..."
sleep $n
echo "$0 exiting ..."
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; for ((i = 2; i = 10; ++i)); do cp job_1 job_$i; done
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; cat jobs.sh
#!/bin/bash
nParellel=5
nJobs=10
sJobPattern='./job_%d'
aJobs=()
sNextJob=
for ((iNextJob = 1; iNextJob = nJobs; )); do
    for ((iJob = 0; iJob nParellel; ++iJob)); do
        if [ $iNextJob -gt $nJobs ]; then
            break;
        fi
        if [ ! "${aJobs[iJob]}" ] || ! kill -0 ${aJobs[iJob]} 2> /dev/null; then
            printf -v sNextJob "$sJobPattern" $((iNextJob++))
            echo "$sNextJob starting ..."
            $sNextJob
            aJobs[iJob]=$!
        fi
    done
    sleep .1
done
wait
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; ./jobs.sh
./job_1 starting ...
./job_1 sleeping for 3 seconds ...
./job_2 starting ...
./job_2 sleeping for 2 seconds ...
./job_3 starting ...
./job_3 sleeping for 5 seconds ...
./job_4 starting ...
./job_5 starting ...
./job_4 sleeping for 4 seconds ...
./job_5 sleeping for 2 seconds ...
./job_2 exiting ...
./job_6 starting ...
./job_6 sleeping for 2 seconds ...
./job_5 exiting ...
./job_7 starting ...
./job_7 sleeping for 1 seconds ...
./job_1 exiting ...
./job_8 starting ...
./job_8 sleeping for 3 seconds ...
./job_7 exiting ...
./job_9 starting ...
./job_9 sleeping for 5 seconds ...
./job_4 exiting ...
./job_6 exiting ...
./job_10 starting ...
./job_10 sleeping for 5 seconds ...
./job_3 exiting ...
./job_8 exiting ...
./job_9 exiting ...
./job_10 exiting ...
-(dearvoid@LinuxEden:Forum)-(~/tmp)-
[$$=6718 $?=0] ; bye

標(biāo)簽:通遼 隨州 青島 延邊 雅安 西藏 朝陽(yáng) 南充

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Shell中實(shí)現(xiàn)“多線程”執(zhí)行腳本文件完美解決方案》,本文關(guān)鍵詞  Shell,中,實(shí)現(xiàn),多線程,執(zhí)行,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Shell中實(shí)現(xiàn)“多線程”執(zhí)行腳本文件完美解決方案》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Shell中實(shí)現(xiàn)“多線程”執(zhí)行腳本文件完美解決方案的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章