Stumped on simple BASH Script

[LEFT]Hello All,

First and foremost, if I have posted this question in the wrong forum/section, I apologize.

Okay so here is my dilemma. I have written a BASH script that automatically restarts a tomcat on a given server. That part was simple enough. However, now I would like to not only restart the tomcat, but I would also like the script to check to make sure the Tomcat is running. The only problem is on some of the servers I look after, I have two tomcats running. Therefore, I need to be able to check to make sure that BOTH tomcats are running. (I am aware that if I restart one tomcat, the second tomcat wont necessarily be restarted)

Here is what I have so far:

#!/bin/bash
#Test version 0.1

TOMCAT_STRING="catalina.base=/srv/tomcat "
TOMCAT_RUNNING=":::18080\n:::8080"

pid_check () { # find the PID of the tomcat we want to bounce
    pid=$(ps -ef | grep "${TOMCAT_STRING}" | grep -v grep | grep -v root | awk '{print $2}')
}

tomcat_status_check(){ 
    tomcat_status=$(netstat -na | grep 8080 | awk '/LISTEN/' | awk '{print $4}')
    echo $tomcat_status
    if [ $tomcat_status = $TOMCAT_RUNNING ];
    then 
    echo "BOTH TOMCATS ARE RUNNING"
    else
    echo "TOMCAT NOT RUNNING"
    fi        
}

tomcat_restart () {
    pid_check $pid
    echo $current_status
    echo "PERFORMING KILL -QUIT"
    kill -QUIT ${pid} > /dev/null 2>&1
    echo "SLEEPING 15 SECONDS"
    sleep 15
    echo "PERFORMING KILL -9"
    kill -9 ${pid} > /dev/null 2>&1
    echo "SLEEPING 45 SECONDS"
    sleep 45
    echo "STARTING TOMCAT"
    /srv/tomcat/bin/catalina.sh start
    echo "SLEEPING 150 SECONDS WHILE TOMCAT BOOTS APPS"
    sleep 150
}

#STACK
pid_check
tomcat_restart
tomcat_status_check
echo "SCRIPT COMPLETED"

As I said, the Tomcat restart part works. However, when the tomcat_status_check runs, it encounters this error:

[explorer@oso12d tools]$ ./mscc-test.sh 
:::18080 :::8080
./mscc-test.sh: line 40: [: too many arguments
TOMCAT NOT RUNNING
SCRIPT COMPLETED

Obviously something is wrong with my function or my $TOMCAT_RUNNING variable isn't correct.

When I run the command for the tomcat_status I get:

[explorer@oso12d tools]$ netstat -na | grep 8080 | awk '/LISTEN/' | awk '{print $4}'
:::18080
:::8080

Any ideas?

Thanks so much for the help!!!!

-UNM_LOBO
[/LEFT]

For safety sake I would change

if [ $tomcat_status = $TOMCAT_RUNNING ];

to

if [[ $tomcat_status == $TOMCAT_RUNNING ]];

1 Like

replace if [ $tomcat_status = $TOMCAT_RUNNING ]; with if [[ $tomcat_status = $TOMCAT_RUNNING ]]

Another idea:

NUM_RUNNING=$(netstat -na | grep ":::1*8080.*LISTEN" | wc -l)
case $NUM_RUNNING in
    2) echo "BOTH TOMCATS ARE RUNNING" ;;
    1) echo "ONE TOMCAT RUNNING" ;;
    0) echo "TOMCAT NOT RUNNING" ;;
    *) echo "UNKNOWN TOMCAT STATUS ($NUM_RUNNING)" ;;
esac
1 Like

Try this...

netstat -na | grep -w 8080 | awk '/LISTEN/{print $4}'

You don't need to fo all these to get the pid

pid=$(ps -ef | grep "${TOMCAT_STRING}" | grep -v grep | grep -v root | awk '{print $2}')

Try this if you have pgrep...

pid=$( pgrep tomcat )

I am not sure of the actual process name i.e. tomcat

--ahamed

1 Like

First off,

Thank you all for taking the time to add your responses. I truly appreciate the effort on all of your parts.

Now, taking J-Man's and Chubler_XL's suggestions, I went ahead and changed:

if [ $tomcat_status = $TOMCAT_RUNNING ];

to

if [[ $tomcat_status = $TOMCAT_RUNNING ]]

Now the script is running! However, I guess there is still something wrong with either my $tomcat_status or $TOMCAT_RUNNING because the script is returning this:

[explorer@oso12d tools]$ ./mscc-test.sh 
:::18080 :::8080
TOMCAT NOT RUNNING
SCRIPT COMPLETED

So obviously, the two are NOT equal. Any ideas of how I should modify the strings? If not, I might try some brainstorming and incorporate Ahamed101's suggestions.

Thank you

Sorry, don't try my suggestion... I just got the context right...

May be this will do the trick...

TOMCAT_RUNNING=":::18080 :::8080"

--ahamed

You could try replacing

TOMCAT_RUNNING=":::18080\n:::8080"

with

TOMCAT_RUNNING=":::18080
:::8080"
1 Like

Hmm still not working.

I wonder if it is because when:

netstat -na | grep 8080 | awk '/LISTEN/' | awk '{print $4}'

is run, it essentially LISTS two seperate strings, that is:

:::18080
:::8080

Shouldn't that require a \n or a \r?

Or maybe does it need to be transformed into an array?

---------- Post updated at 10:28 PM ---------- Previous update was at 10:26 PM ----------

DING DING DING!!!!

Chubler_XL you are the man!

Thank you and ahamed101 so much!!!!!!

Yes, this whole string matching thing with a CR imbedded is a bit flakey, my suggestion (post #7) should work. But you might be better off counting the lines found like suggested in post #3

For example if you come along later and indent your code to something like this:

    TOMCAT_RUNNING=":::18080
    :::8080"

It will all stop working!

1 Like

Yeah, Chubler_XL suggestion post#3 seems to be a better alternative!

--ahamed

How do I do that ahamed?

You mean -x option? Put set -x in your script after the hash-bang statement. Alternatively you can run the script bash -x <yourscript>

--ahamed

[explorer@oso12d tools]$ ./mscc-test.sh 
:::18080 :::8080
BOTH TOMCATS ARE RUNNING
SCRIPT COMPLETED
[explorer@oso12d tools]$ vi mscc-test.sh 
[explorer@oso12d tools]$ ./mscc-test.sh 

+ TOMCAT_ONE_MESSAGE='TOMCAT-ONE IS RUNNING'
+ TOMCAT_TWO_MESSAGE='TOMCAT-TWO IS RUNNING'
+ TOMCAT_BOTH_MESSAGE='BOTH TOMCATS ARE RUNNING'
+ TOMCAT_NEITHER_MESSAGE='NO TOMCAT IS RUNNING'
+ BOTH_TOMCATS_RUNNING=':::18080
:::8080'
+ TOMCAT_ONE_RUNNING=:::8080
+ TOMCAT_TWO_RUNNING=:::18080
+ tomcat_status_check
++ netstat -na
++ grep 8080
++ awk /LISTEN/
++ awk '{print $4}'
+ tomcat_status=':::18080
:::8080'
+ echo :::18080 :::8080
:::18080 :::8080
+ [[ :::18080
:::8080 == :::18080
:::8080 ]]
+ echo BOTH TOMCATS ARE RUNNING
BOTH TOMCATS ARE RUNNING
+ echo 'SCRIPT COMPLETED'
SCRIPT COMPLETED
[explorer@oso12d tools]$ 

This way you can debug your script... If you remove the solution and execute the script, you will see the difference...

This is where the comparison is happening

+ [[ :::18080 
:::8080 == :::18080 
:::8080 ]]

Good Luck!
--ahamed

You are awesome ahamed. Thank you for your help!