shell script to automatically restart tomcat if stopped

I am using a shell script to start tomcat server if it is not running. I am running this script in cronjob to check it frequently. This is my script

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

if [ "`$SERVICE status`" -eq "$STOPPED_MESSAGE" ]; then
     $SERVICE start
fi

But whenever I run this script, it gives me an error. If tomcat is not running then the error is :

[: ILLEGAL NUMBER : * Tomcat Servlet Engine is not running.]

And if tomcat is running the error is :

[: ILLEGAL NUMBER : * Tomcat Servlet Engine is running with pid 6130.]

I think the error is related to $SERVICE status but I am unable to resolve it. I am a new bee to shell scripting. Please help me out.

I cannot move forward until I resolve this issue.


Source: shell

Leave a Reply