Configure Tomcat service in Linux:
These instructions are related to installing and running Tomcat 7 as a service, which means that it will be launched at system boot and will be closed properly on system shutdown.
Running tomcat as a service in Linux:
For security reasons, you should not run Tomcat as root. It is better to create a user tom or with any name and run Tomcat as that user:
$ sudo adduser tom
Create a file with the script:
$ sudo vim /etc/init.d/tomcat
#!/bin/sh
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Apache Tomcat
# Description: Enable Apache Tomcat service provided by daemon.
### END INIT INFO
ECHO=/bin/echo
TEST=/usr/bin/test
TOMCAT_USER=tom
TOMCAT_HOME=/opt/tomcat
TOMCAT_START_SCRIPT=$TOMCAT_HOME/bin/startup.sh
TOMCAT_STOP_SCRIPT=$TOMCAT_HOME/bin/shutdown.sh
$TEST -x $TOMCAT_START_SCRIPT || exit 0
$TEST -x $TOMCAT_STOP_SCRIPT || exit 0
start()
{
$ECHO -n "Starting Tomcat"
su - $TOMCAT_USER -c "$TOMCAT_START_SCRIPT &"
$ECHO "."
}
stop()
{
$ECHO -n "Stopping Tomcat"
su - $TOMCAT_USER -c "$TOMCAT_STOP_SCRIPT 60 -force &"
while [ "$(ps -fu $TOMCAT_USER | grep java | grep tomcat | wc -l)" -gt "0" ]; do
sleep 5; $ECHO -n "."
done
$ECHO "."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 30
start
;;
*)
$ECHO "Usage: tomcat {start|stop|restart}"
exit 1
esac
exit 0
Make it executable:
$ sudo chmod 755 /etc/init.d/tomcat
Now update the run-levels:
$ sudo update-rc.d tomcat defaults
Adding system startup for /etc/init.d/tomcat …
—-else try this:
sudo ln -s /etc/init.d/tomcat /etc/rc1.d/K99tomcat sudo ln -s /etc/init.d/tomcat /etc/rc2.d/S99tomcat
Start Tomcat service:
$ sudo service tomcat start
Stop Tomcat service:
$ sudo service tomcat stop
Its a good idea to configure Tomcat memory utilization by simply creating a setenv.sh file in tomcat home folder and add following directives:
$ sudo vi /opt/tomcat/bin/setenv.sh JAVA_OPTS="-Xms2048m -Xmx2048m -XX:+HeapDumpOnOutOfMemoryError -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=1024m $JAVA_OPTS"
export CATALINA_PID=”$CATALINA_HOME/catalina_pid.txt”
Configure on Redhat / CentOS:
$ chkconfig tomcat on
