Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have installed Tomcat 7 on Ubuntu 12.04 LTS which run on an Amzon EC2 instance. Now I wish tomcat should restart automatically on system reboot. I read this blog which suggest adding below script to /etc/init.d/tomcat7

# Tomcat auto-start
#
# description: Auto-starts tomcat
# processname: tomcat
# pidfile: /var/run/tomcat.pid

case $1 in
start)
sh /usr/share/tomcat7/bin/startup.sh
;;
stop) 
sh /usr/share/tomcat7/bin/shutdown.sh
;;
restart)
sh /usr/share/tomcat7/bin/shutdown.sh
sh /usr/share/tomcat7/bin/startup.sh
;;
esac 
exit 0

and issue the following commands

sudo chmod 755 /etc/init.d/tomcat7

sudo ln -s /etc/init.d/tomcat7 /etc/rc1.d/K99tomcat

sudo ln -s /etc/init.d/tomcat7 /etc/rc2.d/S99tomcat

sudo /etc/init.d/tomcat7 restart

My Questions

  1. The tomcat7 already has script in it, where do we have to paste the suggested script?
  2. Is the suggested procedure correct?
share|improve this question

migrated from stackoverflow.com Nov 30 '12 at 17:30

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 10 down vote accepted
+50

Create the init script in /etc/init.d/tomcat7 with the contents as per below (your script should work too but I think this one adheres more closely to the standards).

This way Tomcat will start only after network interfaces have been configured.

Init script contents:

#!/bin/bash

### BEGIN INIT INFO
# Provides:        tomcat7
# Required-Start:  $network
# Required-Stop:   $network
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start/Stop Tomcat server
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin

start() {
 sh /usr/share/tomcat7/bin/startup.sh
}

stop() {
 sh /usr/share/tomcat7/bin/shutdown.sh
}

case $1 in
  start|stop) $1;;
  restart) stop; start;;
  *) echo "Run as $0 <start|stop|restart>"; exit 1;;
esac

Change its permissions and add the correct symlinks automatically:

chmod 755 /etc/init.d/tomcat7
update-rc.d tomcat7 defaults

And from now on it will be automatically started and shut down upon entering the appropriate runlevels. You can also control it with service tomcat7 <stop|start|restart>

share|improve this answer
    
Thanks. I am new to Ubuntu. What is the meaning when you say " you Tomcat will start only after network interfaces have been configured." What is the meaning of "network interfaces?" What is the meaning of "upon entering the appropriate run level"? and the file tomcat7 already has lot of script in it where should I add the script suggested by you. –  Gaurav Agarwal Dec 6 '12 at 21:00
    
The init scripts under /etc/init.d can be dependent on each other, such as in this case: tomcat7 script won't be run before the network scripts are run. Could you look up 'runlevels' and 'network interface' terms on google? I'm happy to help with other things but not so much with Googling trivial stuff for others. And about the other question: my script is meant to replace /etc/init.d/tomcat7. –  Marcin Kaminski Dec 7 '12 at 0:54
    
The /etc/init.d/tomcat7 in my system is similar to the file here gist.github.com/942150. Should I remove all the content from the file and paste your script or paste it at some place in the already written script. –  Gaurav Agarwal Dec 7 '12 at 11:56
    
I haven't seen the script you just referenced. It looks sane and it looks like it replaces the startup/shutdown scripts that come with Tomcat. My version is merely using them. Have you tested those scripts and confirm they work? If so, you can go ahead and use my script. Use whichever you like, the important thing is to understand what the script does when you paste it. –  Marcin Kaminski Dec 7 '12 at 14:23

apache tomcat does not ship any init script with it.

  1. Install the prepackaged version maintained by Ubuntu from the Ubuntu package manager this version ships its own init script.

  2. Follow the steps in the blog you refered to which provide you with a kickstart init script.

share|improve this answer

Cant this be added to the /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

sleep 10
/usr/share/tomcat7/bin/startup.sh
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.