Ubuntu configures tomcat to start with the System

Source: Internet
Author: User

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

#!/bin/sh## /etc/init.d/tomcat -- startup script for the Tomcat servlet engine## Written by Miquel van Smoorenburg <miquels@cistron.nl>.# Modified for Debian GNU/Linuxby Ian Murdock <imurdock@gnu.ai.mit.edu>.# Modified for Tomcat by Stefan Gybas <sgybas@debian.org>.# Modified for tomcat by Thierry Carrez <thierry.carrez@ubuntu.com>.# Additional improvements by Jason Brittain <jason.brittain@mulesoft.com>.#### BEGIN INIT INFO# Provides:          tomcat# Required-Start:    $local_fs $remote_fs $network# Required-Stop:     $local_fs $remote_fs $network# Should-Start:      $named# Should-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 INFOset -ePATH=/bin:/usr/bin:/sbin:/usr/sbinNAME=tomcatDESC="Tomcat servlet engine"DEFAULT=/etc/default/$NAMEJVM_TMP=/tmp/tomcat-tmpif [ `id -u` -ne 0 ]; thenecho "You need root privileges to run this script"exit 1fi # Make sure tomcat is started with system localeif [ -r /etc/default/locale ]; then. /etc/default/localeexport LANGfi. /lib/lsb/init-functionsif [ -r /etc/default/rcS ]; then. /etc/default/rcSfi# The following variables can be overwritten in $DEFAULT# Run Tomcat as this user ID and group IDtomcat_USER=tomcattomcat_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 usefor jdir in $JDK_DIRS; do    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; thenJAVA_HOME="$jdir"    fidoneexport JAVA_HOME# Directory where the Tomcat binary distribution residesCATALINA_HOME=/usr/local/$NAME# Directory for per-instance configuration files and webappsCATALINA_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 the# Xalan XSL transformer can work without X11 display on JDK 1.4+# It also looks like the default heap size of 64M is not enough for most cases# so the maximum heap size is set to 128Mif [ -z "$JAVA_OPTS" ]; thenJAVA_OPTS="-Djava.awt.headless=true -Xmx128M"fi# End of variables that can be overwritten in $DEFAULT# overwrite settings from default fileif [ -f "$DEFAULT" ]; then. "$DEFAULT"fiif [ ! -f "$CATALINA_HOME/bin/bootstrap.jar" ]; thenlog_failure_msg "$NAME is not installed"exit 1fiPOLICY_CACHE="$CATALINA_BASE/work/catalina.policy"if [ -z "$CATALINA_TMPDIR" ]; thenCATALINA_TMPDIR="$JVM_TMP"fi# Set the JSP compiler if set in the tomcat.default fileif [ -n "$JSP_COMPILER" ]; thenJAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=\"$JSP_COMPILER\""fiSECURITY=""if [ "$tomcat_SECURITY" = "yes" ]; thenSECURITY="-security"fi# Define other required variablesCATALINA_PID="/var/run/$NAME.pid"CATALINA_SH="$CATALINA_HOME/bin/catalina.sh"# Look for Java Secure Sockets Extension (JSSE) JARsif [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then    JSSE_HOME="${JAVA_HOME}/jre/"ficatalina_sh() {# Escape any double quotes in the value of JAVA_OPTSJAVA_OPTS="$(echo $JAVA_OPTS | sed 's/\"/\\\"/g')"AUTHBIND_COMMAND=""if [ "$AUTHBIND" = "yes" -a "$1" = "start" ]; thenJAVA_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" ]; thenTOMCAT_SH="'$TOMCAT_SH'"fi# Run the catalina.sh script as a daemonset +etouch "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.outchown $tomcat_USER "$CATALINA_PID" "$CATALINA_BASE"/logs/catalina.outstart-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 -ereturn $status}case "$1" in  start)if [ -z "$JAVA_HOME" ]; thenlog_failure_msg "no JDK found - please set JAVA_HOME"exit 1fiif [ ! -d "$CATALINA_BASE/conf" ]; thenlog_failure_msg "invalid CATALINA_BASE: $CATALINA_BASE"exit 1filog_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 directoryrm -rf "$JVM_TMP"mkdir -p "$JVM_TMP" || {log_failure_msg "could not create JVM temporary directory"exit 1}chown $tomcat_USER "$JVM_TMP"catalina_sh start $SECURITYsleep 5        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \--user $tomcat_USER --exec "$JAVA_HOME/bin/java" \>/dev/null; thenif [ -f "$CATALINA_PID" ]; thenrm -f "$CATALINA_PID"filog_end_msg 1elselog_end_msg 0fielse        log_progress_msg "(already running)"log_end_msg 0fi;;  stop)log_daemon_msg "Stopping $DESC" "$NAME"set +eif [ -f "$CATALINA_PID" ]; then start-stop-daemon --stop --pidfile "$CATALINA_PID" \--user "$tomcat_USER" \--retry=TERM/20/KILL/5 >/dev/nullif [ $? -eq 1 ]; thenlog_progress_msg "$DESC is not running but pid file exists, cleaning up"elif [ $? -eq 3 ]; thenPID="`cat $CATALINA_PID`"log_failure_msg "Failed to stop $NAME (pid $PID)"exit 1firm -f "$CATALINA_PID"rm -rf "$JVM_TMP"elselog_progress_msg "(not running)"filog_end_msg 0set -e;;   status)set +estart-stop-daemon --test --start --pidfile "$CATALINA_PID" \--user $tomcat_USER --exec "$JAVA_HOME/bin/java" \>/dev/null 2>&1if [ "$?" = "0" ]; thenif [ -f "$CATALINA_PID" ]; then    log_success_msg "$DESC is not running, but pid file exists."exit 1else    log_success_msg "$DESC is not running."exit 3fielselog_success_msg "$DESC is running with pid `cat $CATALINA_PID`"fiset -e        ;;  restart|force-reload)if [ -f "$CATALINA_PID" ]; then$0 stopsleep 1fi$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 startfi        ;;  *)log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"exit 1;;esacexit 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"




Shell> update-rc.d Tomcat ults



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.