2012-06-19 86 views

回答

1

當你用腳本的文件夾中運行這個bash腳本/命令:

for var in $(ls *.pl) 
do 
    $var & 
    # 5 simultaneous jobs 
    while test "$(jobs |wc -l)" -gt 4 
    do 
     sleep 1 
    done 
done 

這依賴於你沒有其它後臺任務運行,通過寫「工作」在命令行上進行測試。

+0

我後來得知您可以使用xargs來運行多個並行作業。使用4個進程同時gzip文件: 'ls * .pl | xargs -n1 -P4 gzip' – Pylinux

2

你可以使用nohup前5個腳本的任何解決方案?讓第五個腳本寫入已完成並繼續的某個文件。

0
You can write sth like below. It's pseudo code. 

#!/bin/ksh 

dir=$1 
count=0 
for script in $1/* 
    do 
    count++ 
    $i & 
    add the pid to pidList array 
    if(count==5){ 
    while(waitforProcessComplete(pidList){ 
    } 
    } 

    done 
0

我寧願GNU並行。這很容易和有趣 - 而且documentation帶有許多簡潔的例子。

最大的優點是:您不必編寫任何代碼或Makefile來運行腳本。示例:

# create the example scripts: 
mkdir scripts 
for i in {1..50}; do {echo '#!/bin/sh'; echo "echo $i"; echo "sleep 1" } > scripts/s$i.sh; done 

# use parallel to run all these scripts, 5 at a time: 
parallel -j 5 ::: scripts/*.sh 
相關問題