Script to add tomcat to start automatically upon reboot

By | August 31, 2017

We need to place the script to add tomcat to start automatically upon reboot inside the init.d directory. Follow the below steps:

cd etc/init.d/
touch  tomcat
vi tomcat

Modify the java version and path as required marked in red in the content which will be pasted in tomcat file, and also the home directory where tomcat start and stop script exists.

Paste the below contents:

#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/local/app/javahome/jdk/jdk1.7.0_79
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/local/app/javahome/apps/customapp
SHUTDOWN_WAIT=20
tomcat_pid() {
echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk ‘{ print $2 }’`
}
start() {
pid=$(tomcat_pid)
if [ -n “$pid” ]
then
echo “Tomcat is already running (pid: $pid)”
else
# Start tomcat
echo “Starting tomcat”
/bin/su -p -s /bin/sh javaadmin $CATALINA_HOME/bin/start_tomcat
fi

return 0
}

stop() {
pid=$(tomcat_pid)
if [ -n “$pid” ]
then
echo “Stoping Tomcat”
/bin/su -p -s /bin/sh applicationadmin $CATALINA_HOME/bin/stop_tomcat

let kwait=$SHUTDOWN_WAIT
count=0;
until [ `ps -p $pid | grep -c $pid` = ‘0’ ] || [ $count -gt $kwait ]
do
echo -n -e “\nwaiting for processes to exit”;
sleep 1
let count=$count+1;
done

if [ $count -gt $kwait ]; then
echo -n -e “\nkilling processes which didn’t stop after $SHUTDOWN_WAIT seconds”
kill -9 $pid
fi
else
echo “Tomcat is not running”
fi

return 0
}

case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
pid=$(tomcat_pid)
if [ -n “$pid” ]
then
echo “Tomcat is running with pid: $pid”
else
echo “Tomcat is not running”
fi
;;
esac
exit 0

Next follow the below steps to add tomcat to startup(chkconfig)

chmod 755 tomcat
chkconfig –add tomcat

Start and check to see tomcat is started and working as expected and go ahead with reboot and check the tomcat service is started upon reboot, handover the server to requester

/etc/init.d/tomcat start
ps -ef | grep tomcat
reboot

The tomcat service is now active and started upon reboot.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *