With an init script for your Tomcat (or other programs) you can handle two demands:
- You can specify the required options like
JAVA_OTPS
in this script - Your Tomcat will be startet after server restart
In this post I will use a Tomcat for Solr as example. So first we need to define a init script:
$ touch /etc/init.d/solr $ chmod a+x /etc/init.d/solr
The content of /etc/inid.d/solr
looks like this:
#! /bin/sh ### BEGIN INIT INFO # Provides: tomcat-solr # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Solr # Description: Solr Tomcat ### END INIT INFO NAME="solr" TOMCAT_DIR="/opt/tomcat-solr" export JAVA_OPTS="-Dsolr.solr.home=/my-solr-home-dir" export CATALINA_PID="$TOMCAT_DIR/catalina.pid" case "$1" in start) $TOMCAT_DIR/bin/startup.sh ;; stop) $TOMCAT_DIR/bin/shutdown.sh -force ;; restart) $0 stop $0 start ;; status) if [ -f $CATALINA_PID ] && ps --no-heading -p `cat $CATALINA_PID`; then echo "$NAME is running" else echo "$NAME is not running" fi ;; *) echo "Usage: /etc/init.d/$NAME {start|stop|restart|status}" >&2 exit 1 ;; esac exit 0
Now we can test the script with service solr status
Last step is to create the required symlinks for the several runlevels. This can be done with update-rc.d
:
$ update-rc.d solr defaults 99 Adding system startup for /etc/init.d/solr ... /etc/rc0.d/K99solr -> ../init.d/solr /etc/rc1.d/K99solr -> ../init.d/solr /etc/rc6.d/K99solr -> ../init.d/solr /etc/rc2.d/S99solr -> ../init.d/solr /etc/rc3.d/S99solr -> ../init.d/solr /etc/rc4.d/S99solr -> ../init.d/solr /etc/rc5.d/S99solr -> ../init.d/solr
Used Versions: Ubuntu 12, Tomcat 7
References: