0

Infinite loop on bash script and I want to run forever but (I guess) something goes wrong script is killed. Is there any way like try-catch, just continue to running forever, unconditionaly.

#!/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
3
  • 4
    Try help trap from the bash prompt. Commented Apr 4, 2011 at 5:55
  • I just add this line, waiting for a while. Thanks trap "echo do_nothing" SIGINT SIGTERM Commented Apr 4, 2011 at 6:04
  • 1
    What have you called your script? Is it possible that ps -ef | grep java matches your script as well? Try using pgrep java or ps -C java instead of ps -ef | grep java. Commented Apr 4, 2011 at 11:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.