Running Tomcat as a service in Linux:
For security reasons you should not run Tomcat as root service. It is better to create a new user and run Tomcat under it.
Create a file with the script:
$ sudo vi /etc/init.d/tomcat
(add following and set path of tomcat home directory):
#!/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=openkm TOMCAT_HOME=/home/openkm/tomcat-8.0.32 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 with this command:
$ sudo chmod 755 /etc/init.d/tomcat
update the run-levels:
$ sudo update-rc.d tomcat defaults
Adding system startup for /etc/init.d/tomcat ... /etc/rc0.d/K20tomcat -> ../init.d/tomcat /etc/rc1.d/K20tomcat -> ../init.d/tomcat /etc/rc6.d/K20tomcat -> ../init.d/tomcat /etc/rc2.d/S20tomcat -> ../init.d/tomcat /etc/rc3.d/S20tomcat -> ../init.d/tomcat /etc/rc4.d/S20tomcat -> ../init.d/tomcat /etc/rc5.d/S20tomcat -> ../init.d/tomcat
If above commands do not work you can also 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
It is 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 export JAVA_OPTS="-Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=356m -XX:PermSize=256m -XX:MaxPermSize=356m" export CATALINA_PID="$CATALINA_HOME/catalina_pid.txt"