Ubuntu: exit shell script when a process check is failed



Question:

i need to exit from shell script when TESTEXCHANGE process is not running on a host.How can i handle this:

I have a script processcheck which checks if the process is running.

I call that to the MainTest script.I want to exit MainTest script if the ProcessCheck script fails.

cat Processcheck:

while read proc; do          if (( 0 == $(ps -ef | grep -v grep | grep -c /location) )); then                  echo "!!!!! $proc down on Master TU  !!!!!"                  RC=1           else             echo "!!!!!! $proc is still running on Master TU!!!!!"          fi  #echo "Not checked"    done < /tmp/PROCESSLIST  

cat MainTest:

#!/bin/bash    cd /app/utp/bin    ssh Test@TestHost 'bash -s' < /ProcessCheck  

i want to exit here if processcheck fails next steps


Solution:1

You could use exit statuses to deal with your case. Exit statuses are numerical values returned by a command to indicate whether the execution went well or not. The default value is 0 if it was a success, a number between [1..255] otherwise. Each value has a signification.

For instance, you could rewrite your Processcheck like this :

while read proc; do          if (( 0 == $(ps -ef | grep -v grep | grep -c /location) )); then                  echo "!!!!! $proc down on Master TU  !!!!!"                  RC=1  #I'm not sure why you use this line                  EXIT_STATUS=1           else             echo "!!!!!! $proc is still running on Master TU!!!!!"          fi  #echo "Not checked"    done < /tmp/PROCESSLIST  exit $EXIT_STATUS  #1 means there was an error, 0 means everything went fine  

In your MainTest, you can call Processcheck and check its status by looking the variable $?


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »