Tomcat as Linux Service

How to configure tomcat to run as a Linux service.Open a terminal and run the following commands

cd /etc/init.d
vi tomcat

write in file

#!/bin/bash
#
# Startup script for Jakarta Tomcat
#
# chkconfig: 345 84 16
# description: Jakarta Tomcat Java Servlet/JSP Container

# Change path
tomcat=/development/_SERVERS/tomcat-simpleĀ  
startup=$tomcat/bin/startup.sh
shutdown=$tomcat/bin/shutdown.sh

start() {
  echo -n $"Starting Tomcat service: "
  sh $startup
  echo $?
}

stop() {
  echo -n $"Stopping Tomcat service: "
  sh $shutdown
  echo $?
}

restart() {
  stop
  start
}

status() {
  ps -aef | grep tomcat-simple | grep -v tomcat6 | grep -v grep
}

# Handle the different input options
case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
status)
  status
  ;;
restart)
  restart
  ;;
*)
  echo $"Usage: $0 {start|stop|restart|status}"
  exit 1
esac

exit 0

save file (ESC:wq) and run the commands

chmod +x tomcat
chkconfig --add tomcat 
sysv-rc-conf tomcat on

In some Linux distributions, last command has to be

chkconfig tomcat on

instead of

sysv-rc-conf tomcat on

in my Linux (Ubuntu 12.04 LTS Desktop), this change is not necessary

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s