2011-04-04 130 views
0

bash腳本上的無限循環,我想永遠運行,但是(我猜)出錯的腳本被殺死了。有沒有什麼辦法像嘗試趕上,只是繼續永遠運行,無條件的。對bash腳本的錯誤處理

#!/bin/bash 


iteration=0 

for ((; ;)) 
do 
    process_id=`ps -ef | grep java | grep TEST | awk '{print $2}' ` 
    kill_command='kill -3 '$process_id 
    time=`date | awk '{print substr($4,0,5)}' ` 
    last_write=`ls -l /files/*.txt | awk '{print $8}' ` 

    if [ "$time" != "$last_write" ] 
    then 
      $kill_command 
      sleep 1 

      $kill_command 
      sleep 1 

      $kill_command 
      sleep 1 

      /test/show_queue.sh 
    fi 

    let "iteration+=1" 

    if [ "$iteration" == "30" ] 
    then 
      let "iteration=0" 
      $kill_command 
      echo '------------' >> memory_status.log 
      date >> memory_status.log 
      prstat -n 7 1 1 >> memory_status.log 
      echo '------------' >> memory_status.log 
      /test/show_queue.sh 
    fi 

    sleep 60 
    done 
+4

從bash提示符處嘗試'help trap'。 – msandiford 2011-04-04 05:55:27

+0

我只是加了這條線,等了一會兒。感謝'陷阱「回聲do_nothing」SIGINT SIGTERM' – 2011-04-04 06:04:54

+1

你叫什麼你的腳本?有可能是'ps -ef | grep java'匹配你的腳本呢?嘗試使用'pgrep java'或'ps -C java'而不是'ps -ef | grep java'。 – Mikel 2011-04-04 11:06:25

回答

1

一個非常簡單的方法是使用兩個腳本。一個與循環和一個誰做殺戮任務:

for ((; ;)) 
do 
    DoKillingTask 
    rc=$? # <- You get the return code of the script and decide what to do 
done 

如果它繼續被殺害,米克爾(在你的問題的意見)是正確的。