Stumped on simple BASH Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stumped on simple BASH Script
# 1  
Old 11-16-2011
Stumped on simple BASH Script

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:

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

Code:
[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:

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

Any ideas?

Thanks so much for the help!!!!

-UNM_LOBO
# 2  
Old 11-16-2011
For safety sake I would change

if [ $tomcat_status = $TOMCAT_RUNNING ];

to

if [[ $tomcat_status == $TOMCAT_RUNNING ]];
This User Gave Thanks to J-Man For This Post:
# 3  
Old 11-16-2011
replace if [ $tomcat_status = $TOMCAT_RUNNING ]; with if [[ $tomcat_status = $TOMCAT_RUNNING ]]


Another idea:
Code:
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

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 11-16-2011
Try this...
Code:
netstat -na | grep -w 8080 | awk '/LISTEN/{print $4}'

You don't need to fo all these to get the pid
Code:
pid=$(ps -ef | grep "${TOMCAT_STRING}" | grep -v grep | grep -v root | awk '{print $2}')

Try this if you have pgrep...
Code:
pid=$( pgrep tomcat )

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

--ahamed

Last edited by ahamed101; 11-16-2011 at 09:58 PM..
This User Gave Thanks to ahamed101 For This Post:
# 5  
Old 11-16-2011
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:

Code:
if [ $tomcat_status = $TOMCAT_RUNNING ];

to

Code:
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:

Code:
[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
# 6  
Old 11-16-2011
Sorry, don't try my suggestion... I just got the context right...

May be this will do the trick...
Code:
TOMCAT_RUNNING=":::18080 :::8080"

--ahamed
# 7  
Old 11-16-2011
You could try replacing
Code:
TOMCAT_RUNNING=":::18080\n:::8080"

with
Code:
TOMCAT_RUNNING=":::18080
:::8080"

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple Bash Script - Crontab

I've put together a very simple bash script to check for software patches and bounce the server, once complete. This is on a Mac server. The script works just fine upon execution, however, cron responds with: /bin/sh: /usr/local/bin/softwareupdates.sh: No such file or directory Crontab: ... (6 Replies)
Discussion started by: Nvizn
6 Replies

2. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

3. Shell Programming and Scripting

Command not found in shell script - stumped for 4 days

Hello, I like to begin with :wall:.. literally... It has been 4 days and I have no idea how to fix it. Environment - AIX 5.3 I wrote a script to call on ssh to log into another box via PKA to do something else. If I run the script on the terminal, it works 100%. If the SAP customised... (11 Replies)
Discussion started by: plonkagain
11 Replies

4. Shell Programming and Scripting

Hopefully a simple script, bash or perl...

I'm attempting to parse a file whose contents follow this format; 4:/eula.1028.txt: 8:/eula.1031.txt: 19:/eula.1033.txt: 23:/eula.1036.txt: 27:/eula.1040.txt: 31:/eula.1041.txt: 35:/eula.1042.txt: 39:/eula.2052.txt: 43:/eula.3082.txt: The number of lines of the file... (4 Replies)
Discussion started by: CudaPrime
4 Replies

5. Shell Programming and Scripting

Simple bash script help

Hi to everyone here, I'm a new user and relatively-new linuxer. I'm trying to write a script that checks if every file from a directory is present in a given list and if not, delete it. should be simple. But I think I've done half the work only: this is to create the reference list: for c... (2 Replies)
Discussion started by: dentex
2 Replies

6. Shell Programming and Scripting

need a simple bash script

to gather the cpu utilization from a system in 5 minute intervals and direct output to file. I'm new at scripting and while this seems like an easy task I'm confused on where to start. thanks for any help (1 Reply)
Discussion started by: mkeyes001
1 Replies

7. Shell Programming and Scripting

simple bash script permission

I have a bash script, but it needs to have a simple protection with password. So if a user wants to run the script , there should be a kdialog to asks for a password.If the password is correct the script starts to run. It should not be the passord of root or another admin user.Just a password in... (2 Replies)
Discussion started by: poort
2 Replies

8. Shell Programming and Scripting

simple bash script

I am writing a shell script in bash one of the thing I want to show is size of export /home du -sk /export/home/oracle | cut -c 1-5 echo "kbytes" when I run the script kbytes shows up in the second line, How can I append kbytes on the same line, such as 61233 kbytes please guide thanks (2 Replies)
Discussion started by: Tirmazi
2 Replies

9. Shell Programming and Scripting

Simple BASH script?

Hi guys, I'm new to the forum so forgive me if I'm sounding ... daft. I currently work in a Tech Support role. Every day we have to generate data by running around 10 .sh scripts. I was thinking instead of having to ./filename 10 times is it possible to right a new script that will run these for... (16 Replies)
Discussion started by: JayC89
16 Replies

10. Shell Programming and Scripting

Simple Bash Script

I'm sure I'm doing something wrong but as I am new to bash shell scripting I'm not sure what: Here's the code webalizer.conf is sitting in the same directory as this file which is named webalizer.sh. Can someone tell me if I've got the syntax right -- it that's correct? I'm executing the... (3 Replies)
Discussion started by: xaphalanx
3 Replies
Login or Register to Ask a Question