tomcat的linux下开机脚本


Let's pretend that ${CATALINA_HOME} is /home/mwood/apache-tomcat-7.0.37. First, in ${CATALINA_HOME}/bin, create a file called setenv.sh and put this in it:

#!/bin/sh

export CATALINA_HOME=/home/mwood/apache-tomcat-7.0.37
export JAVA_HOME=/usr/local/jdk1.7.0_15
export CATALINA_OPTS="-Dreadonly.kvs.pg.username=bd_readonly -Dreadonly.kvs.pg.password=foisgras -Dreadonly.kvs.pg.host=kbdash -Dreadonly.kvs.pg.port=5432 -Dreadonly.kvs.pg.db=kb_2m_kvs"
export CATALINA_PID=${CATALINA_HOME}/var/catalina.pid

Let's pretend you want to run Tomcat as user mwood. Now, as root, create this file in /etc/init.d:

#!/bin/bash

CATALINA_USER=mwood
CATALINA_HOME=/home/mwood/apache-tomcat-7.0.37
CATALINA_BIN="${CATALINA_HOME}/bin"
CATALINA_CMD="${CATALINA_BIN}/catalina.sh"
JAVA_HOME=/usr/local/jdk1.7.0_15
CATALINA_PID=${CATALINA_HOME}/var/catalina.pid

case "$1" in
    start)
        if [ -f "$CATALINA_PID" ]; then
            echo "$CATALINA_PID already exists; process is already running or crashed" 1>&2
        else
            echo "Starting Tomcat..."
            su - $CATALINA_USER -c "${CATALINA_CMD} start"
        fi
        ;;
    status)
        if [ ! -f "$CATALINA_PID" ]; then
            echo "$CATALINA_PID does not exist; process does not exist or has gone rogue"
        else
            echo "pid file exists: ${CATALINA_PID}"
            PID=$(cat $CATALINA_PID)
            echo "should be running with pid ${PID}"
            if [ -x "/proc/${PID}" ]; then
                echo "process exists in process list at /proc/${PID}"
            else
                echo "process does not exist in process list (could not find it at /proc/${PID}; did it die without nuking its own pid file?"
            fi
        fi
        ;;
    stop)
        if [ ! -f "$CATALINA_PID" ]; then
            echo "$CATALINA_PID does not exist; process does not exist or has gone rogue" 1>&2
        else
            PID=$(cat $CATALINA_PID)
            echo "Stopping..."
            su - $CATALINA_USER -c "${CATALINA_CMD} stop"
            while [ -x "/proc/${PID}" ]; do
                echo "Waiting for Tomcat to shut down..."
                sleep 1
            done
            echo "Tomcat stopped"
        fi
        ;;
    *)
        echo "Please use start or stop as first argument" 1>&2
        ;;
esac

Allow Tomcat to Run as Unpriveledged User But Still Serve Port 80

iptables --table nat --append PREROUTING --protocol tcp --destination-port 80 \
    --in-interface eth0 --jump REDIRECT --to-port 8080

NOTE!The above rule will not redirect local requests, since these bypass the PREROUTING chain. Any browsers or other client software running on the server itself will either have to connect directly to port 8080. In most situations, this will be acceptable.

How to Start Tomcat in Remote Debugging Mode

1 Dec 2014

Do this, and you can latch onto your remote tomcat with Eclipse and debug it, at port 8000

$ cd ~/apache-tomcat-7.0.53/bin
$ ./catalina.sh jpda start

你可能感兴趣的:(tomcat的linux下开机脚本)