Tomcat作为Linux系统服务自启动设置

Tomcat作为Linux系统服务自启动设置

一. 简单的方法
最简单的方法就是通过Tomcat自带的startup.sh来自动启动,编辑/etc/rc.d/rc.local,增加:
export JDK_HOME=/usr/local/jdk
export JAVA_HOME=/usr/local/jdk
/usr/local/tomcat/bin/startup.sh
二. 复杂而专业的方法
第一个方法无法指定启动Tomcat服务的用户,存在安全隐患,另外在系统关闭时无法自动停止服务。在Linux下,Tomcat可以作为一个守护进程来启动以及停止,这个必须借助于项目commons-daemon中的jsvc工具。Tomcat安装完后就带有这个工具的源码$CATALINA_HOME/bin/jsvc.tar.gz。首先安装这个工具:
# tar zxvf jsvc.tar.gz
# cd jsvc-src
# chmod +x configure
# ./configure --with-java=/usr/jdk
# make
# ./jsvc -help //检验
完成后是无法用chkconfig --add tomcat设置为自动启动,会显示错误信息:service tomcat does not support chkconfig ,需要修改脚本$CATALINA_HOME/bin/jsvc-src/native/Tomcat.sh,并cp到/etc/rc.d/init.d。我的启动脚本如下(中间出现了几次问题,结果发现都是脚本的问题,所以一定要谨慎):
#!/bin/sh
#
# Startup Script for Tomcat5
#
# chkconfig: 345 87 13
# description: Tomcat Daemon
# processname: jsvc
# pidfile: /var/run/jsvc.pid
# config:
#
# Source function library.
. /etc/rc.d/init.d/functions
#
prog=tomcat
#
# Small shell script to show how to start/stop Tomcat using jsvc
# If you want to have Tomcat running on port 80 please modify the server.xml
# file:
#
# <!-- Define a non-SSL HTTP/1.1 Connector on port 80 -->
# <Connector className="org.apache.catalina.connector.http.HttpConnector"
# port="80" minProcessors="5" maxProcessors="75"
# enableLookups="true" redirectPort="8443"
# acceptCount="10" debug="0" connectionTimeout="60000"/>
#
# You need a developement version of Tomcat (Apache Tomcat/4.1-dev)
#
# Adapt the following lines to your configuration
JAVA_HOME=/usr/local/jdk
CATALINA_HOME=/usr/local/tomcat
DAEMON_HOME=$CATALINA_HOME/bin/jsvc-src
TOMCAT_USER=tomcat
TMP_DIR=/var/tmp
CATALINA_OPTS=
CLASSPATH=$JAVA_HOME/lib/tools.jar:$CATALINA_HOME/bin/commons-daemon.jar:$CATALINA_HOME/bin/bootstrap.jar
start(){
echo -n $"Starting $prog: "
    #
    # Start Tomcat
    #
    $DAEMON_HOME/jsvc \
    -user $TOMCAT_USER \
    -home $JAVA_HOME \
    -Dcatalina.home=$CATALINA_HOME \
    -Djava.io.tmpdir=$TMP_DIR \
    -outfile $CATALINA_HOME/logs/catalina.out \
    -errfile '&1' \
    $CATALINA_OPTS \
    -cp $CLASSPATH \
    org.apache.catalina.startup.Bootstrap
    #
    # To get a verbose JVM
    #-verbose \
    # To get a debug of jsvc.
    #-debug \
    RETVAL=$?
    [ $RETVAL = 0 ] && touch /var/lock/subsys/jsvc
    [ $RETVAL = 0 ] && echo_success || echo_failure
    echo
    return $RETVAL
}
stop(){
    echo -n $"Stopping $prog: "
    #
    # Stop Tomcat
    #
    PID=`cat /var/run/jsvc.pid`
    kill $PID
    RETVAL=$?
    [ $RETVAL = 0 ] && rm /var/lock/subsys/jsvc
    [ $RETVAL = 0 ] && echo_success || echo_failure
    echo
    return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
stop
start
;;
*)
echo "Usage $0 start/stop"
exit 1;;
esac
这样便大功告成了(某些路径、用户设置需要根据自己的具体环境修改)。可能还需要用chown tomcat tomcat及chmod +x tomcat修改属主及可执行权限

你可能感兴趣的:(apache,jdk,tomcat,linux,脚本)