8.11. Starting Tomcat at boot time

In certain situations a server might reboot unexpectedly. It is hence preferable to have Tomcat start automatically when the server starts. To achieve that the first step is to create init scripts. Create a new file called tomcat and paste the below content into it (adjust the HOME variable to your environment):

#Tomcat init script


case $1 in
        sh ${HOME}/startup.sh
        sh ${HOME}/shutdown.sh
        sh ${HOME}/shutdown.sh
        sleep 5
        sh ${HOME}/startup.sh
exit 0

Move the script to the init script directory and make them executable by invoking:

sudo mv tomcat /etc/init.d
sudo chmod +x /etc/init.d/tomcat

Next make sure the tomcat init script will be invoked during system startup and shutdown:

sudo /usr/sbin/update-rc.d -f tomcat defaults 81

Tomcat will now be started at system startup and stopped at system shutdown. If you later need to revert this you can replace defaults with remove and invoke the above commands again.