Ubuntu configures Tomcat to start with the system

Source: Internet
Author: User
Debugging environment: Ubuntu: 11.10 Tomcat: 7.0.14JDK: Ubuntu shell & gt; sudogroupaddtomcatshell & gt; sudouseradd-s/usr/sbin/nol

 

Debugging environment:

 

Ubuntu: 11.10

 

Tomcat: 7.0.14

 

JDK: 1.6.0 -- 20

 

 

 

Shell> tar zxvf apache-tomcat-7.0.14.tar.gz

 

Shell> sudo groupadd tomcat

 

Shell> sudo useradd-s/usr/sbin/nologin-r-g tomcat

 

Shell> sudo cp-r apache-tomcat-7.0.14/usr/local

 

Shell> cd/usr/local

 

Shell> sudo ln-s apache-tomcat-7.0.14 tomcat

 

Shell> sudo chown-R tomcat: tomcat apache-tomcat-7.0.14

 

Shell> sudo chown-R tomcattomcat tomcat

 

Copy the script to/etc/init. d and name it tomcat

 

View plain

#! /Bin/sh

#

#/Etc/init. d/tomcat -- startup script for the Tomcat servlet engine

#

# Written by Miquel van Smoorenburg .

# Modified for Debian GNU/Linux by Ian Murdock .

# Modified for Tomcat by Stefan Gybas .

# Modified for tomcat by Thierry Carrez .

# Additional improvements by Jason Brittain .

#

### BEGIN INIT INFO

# Provides: tomcat

# Required-Start: $ local_fs $ remote_fs $ network

# Required-Stop: $ local_fs $ remote_fs $ network

# Shocould-Start: $ named

# Shocould-Stop: $ named

# Default-Start: 2 3 4 5

# Default-Stop: 0 1 6

# Short-Description: Start Tomcat.

# Description: Start the Tomcat servlet engine.

### END INIT INFO

 

Set-e

 

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

NAME = tomcat

DESC = "Tomcat servlet engine"

DEFAULT =/etc/default/$ NAME

JVM_TMP =/tmp/tomcat-tmp

 

If ['id-U'-ne 0]; then

Echo "You need root privileges to run this script"

Exit 1

Fi

# Make sure tomcat is started with system locale

If [-r/etc/default/locale]; then

./Etc/default/locale

Export LANG

Fi

 

./Lib/lsb/init-functions

 

If [-r/etc/default/rcS]; then

./Etc/default/rcS

Fi

 

 

# The following variables can be overwritten in $ DEFAULT

 

# Run Tomcat as this user ID and group ID

Tomcat_USER = tomcat

Tomcat_GROUP = tomcat

 

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not

# Defined in $ DEFAULT)

JDK _ Dirs = "/usr/local/jvm/java/usr/lib/jvm/java-6-openjdk/usr/lib/jvm/java-6-sun/usr/lib/jvm/java- 1.5.0-sun/usr/lib/j2sdk1.5-sun/usr/lib/j2sdk1.5-ibm"

 

# Look for the right JVM to use

For jdir in $ JDK_DIRS; do

If [-r "$ jdir/bin/java"-a-z "$ {JAVA_HOME}"]; then

JAVA_HOME = "$ jdir"

Fi

Done

Export JAVA_HOME

 

# Directory where the Tomcat binary distribution resides

CATALINA_HOME =/usr/local/$ NAME

 

# Directory for per-instance configuration files and webapps

CATALINA_BASE =/usr/local/$ NAME

 

# Use the Java security manager? (Yes/no)

Tomcat_SECURITY = no

 

# Default Java options

# Set java. awt. headless = true if JAVA_OPTS is not set so

# Xalan XSL transformer can work without X11 display on JDK 1.4 +

# It also looks like the default heap size of 64 M is not enough for most cases

# So the maximum heap size is set to 128 M

If [-z "$ JAVA_OPTS"]; then

JAVA_OPTS = "-Djava. awt. headless = true-Xmx128M"

Fi

 

# End of variables that can be overwritten in $ DEFAULT

 

# Overwrite settings from default file

If [-f "$ DEFAULT"]; then

. "$ DEFAULT"

Fi

 

If [! -F "$ CATALINA_HOME/bin/bootstrap. jar"]; then

Log_failure_msg "$ NAME is not installed"

Exit 1

Fi

 

POLICY_CACHE = "$ CATALINA_BASE/work/catalina. policy"

 

If [-z "$ CATALINA_TMPDIR"]; then

CATALINA_TMPDIR = "$ JVM_TMP"

Fi

 

# Set the JSP compiler if set in the tomcat. default file

If [-n "$ JSP_COMPILER"]; then

JAVA_OPTS = "$ JAVA_OPTS-Dbuild. compiler = \" $ JSP_COMPILER \""

Fi

 

SECURITY = ""

If ["$ tomcat_SECURITY" = "yes"]; then

SECURITY = "-security"

Fi

 

# Define other required variables

CATALINA_PID = "/var/run/$ NAME. pid"

CATALINA_SH = "$ CATALINA_HOME/bin/catalina. sh"

 

# Look for Java Secure Sockets Extension (JSSE) JARs

If [-z "$ {JSSE_HOME}"-a-r "$ {JAVA_HOME}/jre/lib/jsse. jar"]; then

JSSE_HOME = "$ {JAVA_HOME}/jre /"

Fi

 

Catalina_sh (){

# Escape any double quotes in the value of JAVA_OPTS

JAVA_OPTS = "$ (echo $ JAVA_OPTS | sed's/\"/\ "/g ')"

 

AUTHBIND_COMMAND = ""

If ["$ AUTHBIND" = "yes"-a "$1" = "start"]; then

JAVA_OPTS = "$ JAVA_OPTS -Djava.net. preferIPv4Stack = true"

AUTHBIND_COMMAND = "/usr/bin/authbind -- deep/bin/bash-c"

Fi

 

# Define the command to run Tomcat's catalina. sh as a daemon

# Set-a tells sh to export assigned variables to spawned shells.

TOMCAT_SH = "set-a; JAVA_HOME = \" $ JAVA_HOME \ "; source \" $ DEFAULT \";\

CATALINA_HOME = \ "$ CATALINA_HOME \";\

CATALINA_BASE = \ "$ CATALINA_BASE \";\

JAVA_OPTS = \ "$ JAVA_OPTS \";\

CATALINA_PID = \ "$ CATALINA_PID \";\

CATALINA_TMPDIR = \ "$ CATALINA_TMPDIR \";\

LANG = \ "$ LANG \"; JSSE_HOME = \ "$ JSSE_HOME \";\

Cd \ "$ CATALINA_BASE \";\

\ "$ CATALINA_SH \" $ @"

 

If ["$ AUTHBIND" = "yes"-a "$1" = "start"]; then

TOMCAT_SH = "'$ TOMCAT_SH '"

Fi

 

# Run the catalina. sh script as a daemon

Set + e

Touch "$ CATALINA_PID" "$ CATALINA_BASE"/logs/catalina. out

Chown $ tomcat_USER "$ CATALINA_PID" "$ CATALINA_BASE"/logs/catalina. out

Start-stop-daemon -- start-B-u "$ tomcat_USER"-g "$ tomcat_GROUP "\

-C "$ tomcat_USER"-d "$ CATALINA_TMPDIR"-p "$ CATALINA_PID "\

-X/bin/bash ---c "$ AUTHBIND_COMMAND $ TOMCAT_SH"

Status = "$? "

Set + a-e

Return $ status

}

 

Case "$1" in

Start)

If [-z "$ JAVA_HOME"]; then

Log_failure_msg "no JDK found-please set JAVA_HOME"

Exit 1

Fi

 

If [! -D "$ CATALINA_BASE/conf"]; then

Log_failure_msg "invalid CATALINA_BASE: $ CATALINA_BASE"

Exit 1

Fi

 

Log_daemon_msg "Starting $ DESC" "$ NAME"

If start-stop-daemon -- test -- start -- pidfile "$ CATALINA_PID "\

-- User $ tomcat_USER -- exec "$ JAVA_HOME/bin/java "\

>/Dev/null; then

 

# Regenerate POLICY_CACHE file

# Umask 022

# Echo "// AUTO-generated file from/etc/tomcat/policy. d /"\

#> "$ POLICY_CACHE"

# Echo ""> "$ POLICY_CACHE"

# Cat $ CATALINA_BASE/conf/policy. d/*. policy \

#>> "$ POLICY_CACHE"

 

# Remove/recreate JVM_TMP directory

Rm-rf "$ JVM_TMP"

Mkdir-p "$ JVM_TMP" | {

Log_failure_msg "cocould not create JVM temporary directory"

Exit 1

}

Chown $ tomcat_USER "$ JVM_TMP"

 

Catalina_sh start $ SECURITY

Sleep 5

If start-stop-daemon -- test -- start -- pidfile "$ CATALINA_PID "\

-- User $ tomcat_USER -- exec "$ JAVA_HOME/bin/java "\

>/Dev/null; then

If [-f "$ CATALINA_PID"]; then

Rm-f "$ CATALINA_PID"

Fi

Log_end_msg 1

Else

Log_end_msg 0

Fi

Else

Log_progress_msg "(already running )"

Log_end_msg 0

Fi

;;

Stop)

Log_daemon_msg "Stopping $ DESC" "$ NAME"

 

Set + e

If [-f "$ CATALINA_PID"]; then

Start-stop-daemon -- stop -- pidfile "$ CATALINA_PID "\

-- User "$ tomcat_USER "\

-- Retry = TERM/20/KILL/5>/dev/null

If [$? -Eq 1]; then

Log_progress_msg "$ DESC is not running but pid file exists, cleaning up"

Elif [$? -Eq 3]; then

PID = "'cat $ CATALINA_PID '"

Log_failure_msg "Failed to stop $ NAME (pid $ PID )"

Exit 1

Fi

Rm-f "$ CATALINA_PID"

Rm-rf "$ JVM_TMP"

Else

Log_progress_msg "(not running )"

Fi

Log_end_msg 0

Set-e

;;

Status)

Set + e

Start-stop-daemon -- test -- start -- pidfile "$ CATALINA_PID "\

-- User $ tomcat_USER -- exec "$ JAVA_HOME/bin/java "\

>/Dev/null 2> & 1

If ["$? "=" 0 "]; then

 

If [-f "$ CATALINA_PID"]; then

Log_success_msg "$ DESC is not running, but pid file exists ."

Exit 1

Else

Log_success_msg "$ DESC is not running ."

Exit 3

Fi

Else

Log_success_msg "$ DESC is running with pid 'cat $ CATALINA_PID '"

Fi

Set-e

;;

Restart | force-reload)

If [-f "$ CATALINA_PID"]; then

$0 stop

Sleep 1

Fi

$0 start

;;

Try-restart)

If start-stop-daemon -- test -- start -- pidfile "$ CATALINA_PID "\

-- User $ tomcat_USER -- exec "$ JAVA_HOME/bin/java "\

>/Dev/null; then

$0 start

Fi

;;

*)

Log_success_msg "Usage: $0 {start | stop | restart | try-restart | force-reload | status }"

Exit 1

;;

Esac

 

Exit 0

 

 

 

1. if the tomcat file name does not have a soft connection, modify it.

 

NAME = tomcat

 

2. if the user name and group name are not tomcat, modify them.

 

# Run Tomcat as this user ID and group ID

 

Tomcat_USER = tomcat

 

Tomcat_GROUP = tomcat

 

3. if JAVA_HOME is not present this time, add it or set JAVA_HOME in your environment variables.

 

JDK _ Dirs = "/usr/local/jvm/java/usr/lib/jvm/java-6-openjdk/usr/lib/jvm/java-6-sun/usr/lib/jvm/java- 1.5.0-sun/usr/lib/j2sdk1.5-sun/usr/lib/j2sdk1.5-ibm"

 

 

 

Continue

 

 

 

Shell> update-rc.d tomcat ULTS

From the heardy column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.