0

I have an issue with this shell script that I cannot seem to find. I am 100% certain it is something obvious, simple and stupid but I am stumped.

here is the script

# /bin/sh
SERVICE=/etc/init.d/tomcat
STOPPED_MESSAGE="Tomcat is not running"

if [ "$SERVICE status" == "$STOPPED_MESSAGE"];
then
{
   $SERVICE start
}
fi

the error I get when running it is this.

[root in /istorelogs]# sh recover-tomcat.sh 
recover-tomcat.sh: line 5: [: missing `]'

Any help would be greatly appreciated. Thanks

So to see if the if statement was working correctly I added a command to copy the catalina.out to another directory before starting tomcat back up. Here is the updated script. And the output I get from service tomcat status when tomcat is down.

#! /bin/sh
SERVICE=/etc/init.d/tomcat
STOPPED_MESSAGE="Tomcat is not running"

if [ "$(${SERVICE} status)" == "$STOPPED_MESSAGE" ];
then
{
  cp /usr/local/tomcat/logs/catalina.out /istorelogs/cattest.out
  $SERVICE start
}
fi


[root in /istorelogs]# service tomcat status
Tomcat is not running

I tried both methods listed below without any luck. Then I tried this.

#! /bin/sh
SERVICE=/etc/init.d/tomcat
STOPPED_MESSAGE="Tomcat is not running"
if (( $(service tomcat status) = "Tomcat is not running" ));
then
{
  cp /usr/local/tomcat/logs/catalina.out /istorelogs/cattest.out
  $SERVICE start
}
fi

Then I get this output, which leads me to believe I just have a slight syntax issues to overcome with this method. Not sure what it is though.

recover-tomcat.sh: line 4: ((: Tomcat is not running = Tomcat is not running : syntax error: operand expected (error token is "Tomcat is not running = Tomcat is not running ")

Ok I got the script working when running manually, but it does not appear to work when it runs out of cron. here is the working script.

#! /bin/sh
TOMSTATUS=$(service tomcat status)
STOPPED_MESSAGE="Tomcat is not running"
SERVICE=/etc/init.d/tomcat
echo $TOMSTATUS
echo $STOPPED_MESSAGE

if [ "${TOMSTATUS}" = "${STOPPED_MESSAGE}" ];
then
{
  cp /usr/local/tomcat/logs/catalina.out /istorelogs/cattest.out
  $SERVICE start
}
fi

Here is the cron entry for the script, it is atttempting to run everyminute and is according to the cron log, but its not performing as it should

* * * * * sh /istorelogs/recover-tomcat.sh &

1 Answer 1

1

You must call the program SERVICE in a subshell and add a space before the ].

if [ "$(${SERVICE} status)" == "${STOPPED_MESSAGE}" ];

Edit:
You might want to store the status in a var before testing it:

tomstatus=$(${SERVICE} status | strings)
echo "Debug: Status=${tomstatus}."
if [ "${tomstatus}" = "${STOPPED_MESSAGE}" ]; then

I added strings, perhaps there are control characters turning the word tomcat blue.

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

5 Comments

That seems to have remedied the error when running the script. I have shut tomcat down and ran it, but it is not bringing it back up. I matched the variable STOPPED_MESSAGE to the output giving when I run "service tomcat status" when tomcat is off. Any further ideas? Really need to get the up and running to minimize downtime while we figure out what is causing tomcat to crash
See update and compare your Commandline call: service tomcat status with the implemented script call: tomcat status
I added more to it. I tried both methods you suggested without any luck. So then i tried the above. I think i am getting further just have a slight syntax error to overcome
Just use square brackets with your if-statement.
$(service tomcat status) returns a string with spaces. Please put quotes around it: if [ "$(service tomcat status)" = "$STOPPED_MESSAGE" ]

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.