[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8956
  • Last Modified:

Debian Linux : how to autostart tomcat on bootup

hello experts

i have manually installed tomcat server 5 at /usr/local/tomcat
on a ubuntu machine and another debinac machine
i  want to start tomcat server automatically on reboot of the machine

i googled but the help is for tomcat 4 versions where TOMCAT_HOME variable is used
now in my server CATALINA_HOME is used
so i'm confused
pls provide the script for tomcat 5 server


pls guide
thanks
jags
0
Econify
Asked:
Econify
  • 5
  • 5
  • 4
2 Solutions
 
ahoffmannCommented:
your tomcat should come with a sample rc-script
copy this script to the proper rc-directory (/etc/init.d/ usually) then enable the startup with the proper tools (IIRC it is sysconfig on RH)
0
 
EconifyAuthor Commented:
hi hoff man
no idea bat what u r saying
rc-script????

copy this script to the proper rc-directory (/etc/init.d/ usually) then enable the startup with the proper tools (IIRC it is sysconfig on RH)

can u pls elaborate on this
0
 
ahoffmannCommented:
rc-scripts are scripts used by Unix/linux while booting. Each script must have at least 2 modes: start and stop.
These "modes" are passed as parameter to the script.
If tomcat does not come with such a script, use the sample one you find in /etc/init.d and adapt it for tomcat, giv it a unique name and enable it with the proper RH tool (sysconfig).
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
verborghsCommented:
Attached the script i use to start tomcat5 on bootup

1) create a system user tomcat5 (useradd or adduser)
2) change script paramater (leave CATALINA_BASE is unnesecairy)
3) copy script to /etc/init.d/tomcat5
4) use update-rc.d(or rc.d-update ;)) to create the symlinks :
 update-rc.d tomcat5 defaults 90

-----SNIP-----
#! /bin/sh
#
# skeleton      Example initscript
#            This file should be used to construct scripts to be
#            placed in /etc/init.d.
#
# Author:      Miquel van Smoorenburg <miquels@cistron.nl>.
#            Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
#            Please remove the "Author" lines above and replace them
#            with your own name if you copy and modify this script.
#
# Version:      @(#)skeleton  2.85-23  28-Jul-2004  miquels@cistron.nl
#

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="servlet container"
NAME=tomcat5
CATALINA_HOME=/srv/apache-tomcat-5.5.12
CATALINA_BASE=/srv/projects/rmp
DAEMON_START="${CATALINA_HOME}/bin/startup.sh"
DAEMON_STOP="${CATALINA_HOME}/bin/shutdown.sh"
DAEMON_CONTROL="${CATALINA_HOME}/bin/catalina.sh"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
TOMCAT_USER=tomcat5

export CATALINA_HOME CATALINA_BASE

# Gracefully exit if the package has been removed.
test \( -x $DAEMON_START -a -x $DAEMON_STOP -a -x  $DAEMON_CONTROL \)  || exit 0

# Read config file if it is present.
#if [ -r /etc/default/$NAME ]
#then
#      . /etc/default/$NAME
#fi

#
#      Function that starts the daemon/service.
#
d_start() {
      #start-stop-daemon --start --quiet --pidfile $PIDFILE \
      #      --exec $DAEMON
      su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_START"
}

#
#      Function that stops the daemon/service.
#
d_stop() {
      #start-stop-daemon --stop --quiet --pidfile $PIDFILE \
      #      --name $NAME
      su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_STOP"
}

#
#      Function that sends a SIGHUP to the daemon/service.
#
d_debug() {
      #start-stop-daemon --stop --quiet --pidfile $PIDFILE \
      #      --name $NAME --signal 1
      su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_CONTROL run"
}

case "$1" in
  start)
      echo -n "Starting $DESC: $NAME"
      d_start
      echo "."
      ;;
  stop)
      echo -n "Stopping $DESC: $NAME"
      d_stop
      echo "."
      ;;
  debug)
      echo -n "starting $DESC in debug mode: $NAME"
      d_debug
      echo "done."
  ;;
  restart)
      echo -n "Restarting $DESC: $NAME"
      d_stop
      sleep 1
      d_start
      echo "."
      ;;
  *)
      # echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
      echo "Usage: $SCRIPTNAME {start|stop|restart|debug}" >&2
      exit 1
      ;;
esac

exit 0
---SNIP----
0
 
verborghsCommented:
update:
you might also do a chown tomcat5 -R <CATALINA_HOME> to make sure tomcat can read everything
0
 
ahoffmannCommented:
su in an rc-script, is tomcat that stupid?
also, if su is really necessary, I'd use it without - 'cause the shell profiles may contain dangerous things (like stty, etc.)
0
 
verborghsCommented:
su in an rc-script, is tomcat that stupid?
euhm, java doesn't know how todrop privelegdes and you don't want tomcat to run as root.

the '-' allows me to specify the custom path variable (exclude binaries/specify alternative JVM/....) and if someone is able to add content to the .profile you have bigger problems to worry about.
0
 
EconifyAuthor Commented:
hi verborghs
u script is absolutely amazing
hey other guys this is way to answer(please guys be a little elaborate)

thanks a ton for that

can u explain me what is

update-rc.d tomcat5 defaults 90

and will this script shutdown the server properly in case there is a halt or reboot

thanks again
0
 
ahoffmannCommented:
> .. java doesn't know how todrop privelegdes ..
LOL, another reason to switch back to reliable shell scripting ;-)

> .. you have bigger problems to worry about.
agreed, my intent was to point to these problems.
0
 
verborghsCommented:
update-rc.d makes links from the needed rcX.d folders. the folders control what gets starten in which runlevel. runlevels are like safe-boot/networking/no-networking/normal choice you have when booting windows only better ;)

because of the names of the links that get created your linux system will call the script with start or stop as an argument, so yes, your tomcat will be shutdown as it shoul
0
 
EconifyAuthor Commented:
hi verborghs

there's a small error in ur script
su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_CONTROL run"
shud be
su - $TOMCAT_USER -s /bin/bash -c "$DAEMON_CONTROL" run
other wise u get the error not a directory

and hey i just did a shutdown -r now
and the tomcat did not start automaticaly on bootup
0
 
EconifyAuthor Commented:
i checked whether the link is there
and its there
debinac:~# update-rc.d tomcatstartnew defaults 90
 System startup links for /etc/init.d/tomcatstartnew already exist.
debinac:~#
whats 90 for ??
0
 
ahoffmannCommented:
> and the tomcat did not start automaticaly on bootup
check your error logs of tomcat

> whats 90 for ??
it's a number used in the name of the link created by update-rc.d, if you look in the /etc/rc.d/rc3.d/ directory you'll see that all scripts are named SNNname and/or KNNname where NN is such a number. While booting scripts are started in sequence of these numbers, same applies for shutdown.
0
 
EconifyAuthor Commented:
thanks dudes
now it works
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now