How to automatically restart Tomcat7 on system reboots?
Create the init script in /etc/init.d/tomcat7 with the contents as per below (your script should work too but I think this one adheres more closely to the standards).
This way Tomcat will start only after network interfaces have been configured.
Init script contents:
#!/bin/bash
### BEGIN INIT INFO
# Provides: tomcat7
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO
PATH=/sbin:/bin:/usr/sbin:/usr/bin
start() {
sh /usr/share/tomcat7/bin/startup.sh
}
stop() {
sh /usr/share/tomcat7/bin/shutdown.sh
}
case $1 in
start|stop) $1;;
restart) stop; start;;
*) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac
Change its permissions and add the correct symlinks automatically:
chmod 755 /etc/init.d/tomcat7
update-rc.d tomcat7 defaults
And from now on it will be automatically started and shut down upon entering the appropriate runlevels. You can also control it with service tomcat7 <stop|start|restart>
Cant this be added to the /etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sleep 10
/usr/share/tomcat7/bin/startup.sh
#!/bin/bash
#
# Author : subz
# Copyright (c) 2k15
#
# Make kill the tomcat process
#
TOMCAT_HOME=/media/subin/works/Applications/apache-tomcat-7.0.57
SHUTDOWN_WAIT=5
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/sh $TOMCAT_HOME/bin/startup.sh
fi
return 0
}
stop() {
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Stoping Tomcat"
/bin/sh $TOMCAT_HOME/bin/shutdown.sh
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
echo " \nprocess killed manually"
fi
else
echo "Tomcat is not running"
fi
return 0
}
pid=$(tomcat_pid)
if [ -n "$pid" ]
then
echo "Tomcat is running with pid: $pid"
stop
else
echo "Tomcat is not running"
start
fi
exit 0