Rajan
Rajan

Reputation: 41

shell script to Start/Stop tomcat server

I am trying to stop and start tomcat using shell script on linux server. Below is shell script i copied from internet but it is not working. I am new for shell scripting. Please can anyone help me out. Thanks in advance.

#!/bin/bash

export BASE=/opt/tomcat/apache-tomcat-8.5.47/bin
prog=apache-tomcat-8.5.47

stat() {
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat is running.
else
echo Tomcat is not running.
fi
}

case "$1" in
start)

if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
echo Tomcat seems to be running. Use the restart option.
else
$BASE/startup.sh 2>&1 > /dev/null
fi
stat
;;
stop)
$BASE/shutdown.sh 2>&1 > /dev/null
if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
stat
;;
restart)

if [ `ps auxwwww|grep $prog|grep -v grep|wc -l` -gt 0 ]
then
for pid in `ps auxwww|grep $prog|grep -v grep|tr -s ' '|cut -d ' ' -f2`
do
kill -9 $pid 2>&1 > /dev/null
done
fi
$BASE/startup.sh 2>&1 > /dev/null
stat
;;
status)
stat
;;
*)
echo "Usage: tomcat start|stop|restart|status"
esac

Upvotes: 1

Views: 8096

Answers (1)

Rushi Daxini
Rushi Daxini

Reputation: 1700

Please follow below step:

  • Save provided code in .sh file (i.e. StartStopScript.sh) at one location.
  • Update export BASE variable with you tomcat bin location.
  • Update prog variable with tomcat version.
  • IMPORTANT: run the script with argument eg.

StartStopScript.sh start to start the server

StartStopScript.sh stop to stop the server

StartStopScript.sh restart to restart the server

StartStopScript.sh status to check server status

Upvotes: 1

Related Questions