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 &