简体   繁体   English

bash脚本上的错误处理

[英]Error Handling on bash script

Infinite loop on bash script and I want to run forever but (I guess) something goes wrong script is killed. bash脚本上的无限循环,我想永远运行,但是(我想)脚本出了问题。 Is there any way like try-catch, just continue to running forever, unconditionaly. 是否有任何类似try-catch的方法,只是继续无条件地永远运行。

#!/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

A very simple way to do it is to use two scripts. 一种非常简单的方法是使用两个脚本。 One with the loop and one which does the killing task : 一个带有循环,另一个带有杀死功能:

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

If it continues to be killed, Mikel (in comment of your question) is right. 如果它继续被杀死,米克尔(在您的问题的评论中)是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM