2014-04-09 90 views
0
sh bothworkfile.txt >> cleanup_process.log 

while [[ `ps -eaf | grep -c "cleanx"` -ge 2 ]] 
do 
sleep 10 
echo "Please be patient, we are still cleaning stuffs ! " 
done 
    echo " Clean up is done." 

嗨我正在使用ksh。 bothworkfile.txt包含幾行用於在後臺運行進程。雖然在ksh循環

cat bothworkfile.txt 
cleanx data01.xsd*.* & 
cleanx data01.xsd*.* & 

Cleanx是我的本地shell中的一個實用程序。默認情況下,總是有一個cleanx在後臺運行。因此,

ps -eaf | grep -c "cleanx" 

總是給出1,但是當腳本調用它時,數字會增加。

但現在的問題是,似乎我無法進入while循環。運行腳本後,它會打印出「清理完成」。

請幫我理解它爲什麼不進入while循環。再次,它的ksh shell。

+0

當我做cleanx data01.xsd * * cleanx data01.xsd * *命令行和運行PS -eaf上| grep -c「cleanx」它顯示3 ...所以'ps -eaf | grep -c「cleanx」'-ge 2是正確的..但它的整個while循環.. – May

回答

1

爲了更容易調試和看到的結果是,你期待什麼真正的ps -eaf | grep -c "cleanx"結果分配給一個變量,然後測試該變量。我懷疑它正在計算空白或小於2,這會導致測試返回false,從而跳過循環。所以,在測試之前將其打印出來,以便您確實知道。 。事情是這樣的:

cleanx_count=$(ps -eaf | grep -c "cleanx") 

printf "Cleanx_count: %d/n" ${cleanx_count} 

while [[ ${cleanx_count} -ge 2 ]] 
+0

謝謝加里!該SH bothworkfile.txt這麼快就完成沒有適當履行其職責,從而cleanx_count couldnot捕捉過程。但是,這個調試信息幫助我瞭解了這一點。非常感謝。 – May