Automate Tomcat startup/shutdown on Linux

If you need to automatically start/stop Tomcat each time you start/stop/reboot your Linux server, you can do so by creating a start/stop script under the /etc/init.d directory.

The sample script below is called /etc/init.d/tomcat and runs Tomcat as a user called “tomcat” – because most likely you do not want Tomcat to run as root.

#!/bin/sh
case "$1" in
  start)
    echo "Starting tomcat"
    su tomcat -c "export JAVA_HOME=/web/java;/web/tomcat/bin/startup.sh"
    ;;
  stop)
    echo "Stopping tomcat"
    su tomcat -c "export JAVA_HOME=/web/java;/web/tomcat/bin/shutdown.sh"
    ;;
  *)
    echo "Usage: /etc/init.d/tomcat {start|stop}"
    exit 1
    ;;
esac
exit 0

After you create the script, you need to set its attributes:
sudo chmod 755 /etc/init.d/tomcat

And then create all the appropriate links to it:
sudo update-rc.d tomcat defaults

If you need to remove the links again (but keep the script), use the following command:
sudo update-rc.d -f tomcat remove

Leave a Reply

Your email address will not be published. Required fields are marked *

*