Start tomcat at startup with administrative privileges

To run a service without or before logging in to the system (i.e. "on boot"), you will need to create a startup script and add it to the boot sequence.
There's three parts to a service script: start, stop and restart.
The basic structure of a service script is:

#!/bin/bash
#
RETVAL=0;

start() {
echo “Starting <Service>”
}

stop() {
echo “Stopping <Service>”
}

restart() {
stop
start
}

case “$1″ in
start)
  start
;;
stop)
  stop
;;
restart)
  restart
;;
*)

echo $”Usage: $0 {start|stop|restart}”
exit 1
esac

exit $RETVAL  

Once you have tweaked the script to your liking, just place it in /etc/init.d/
And, add it to the system service startup process (on Fedora, I am not a Ubuntu user, >D):

chkconfig -add <ServiceName>  

Service will be added to the system boot up process and you will not have to manually start it up again.

Cheers!


Depending on init system, you create init script differently. Fedora gives you upstart and systemd to choose from, and of course SysV compatibility.

Upstart

  • create service definition file as /etc/init/custom-tomcat.conf
  • put inside:

    start on stopped rc RUNLEVEL=3
    respawn
    exec /path/to/your/tomcat --and --parameters
    

And your Tomcat should start on system start.

Systemd

  • create service definition in /etc/systemd/system/custom-tomcat.service
  • put inside:

    [Service]
    ExecStart=/path/to/your/tomcat --and --parameters
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

and enable your service using systemctl enable custom-tomcat.service. It will be started every normal boot.

Of course there are few more configuration options for both init systems, you can check those in their documentation.


Tomcat is a fairly common service, I'd recommend looking at the init script provided by the distro already. Chances are it works with your customized binary, with little to no tweaking.

Tags:

Startup

Init