使用JSVC启动TOMCAT

Tomcat在linux可以把启动代码写到/etc/rc.local里面。此种方式tomcat将以root权限运行,存在安全隐患,推荐jsvc方式启动.

jsvc配置启动:

一:创建tomcat用户

useradd tomcat

二:生成jsvc脚本

1.tomcat的安装目录的bin目录下存在一个jsvc.tar.gz或commons-daemon-native.tar.gz压缩文件,对其解压.

2.执行configure生成jsvs
[root@lijun unix]# ./configure –with-java=/usr/jdk1.6.0_21/
把生成的jsvs拷贝到tomcat的bin下面
3.如果报错
ar: libservice.a: Malformed archive
make[1]: *** [libservice.a] Error 1)
执行make clean后再次make即可,不用make install的(习惯make && make install的注意)
三:将 commons-daemon-native.tar.gz解压目录\unix\native 目录 下的Tomcat5.sh脚本复制到/etc/init.d目录下并重命名为tomcat后做如下修改。
#!/bin/sh
#!/bin/bash
   # chkconfig: 2345 10 90
   # description: Starts and Stops the Tomcat daemon. 
#上述用于添加到chkconfig
##############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the “License”); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an “AS IS” BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# 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″/>
#
# That is for Tomcat-5.0.x (Apache Tomcat/5.0)
#
# Adapt the following lines to your configuration
JAVA_HOME=/usr/jdk1.6.0_21       //java路径
CATALINA_HOME=/usr/tomcat        //tomcat路径
DAEMON_HOME=$CATALINA_HOME
TOMCAT_USER=tomcat                //用户
# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=/usr/tomcat         //tomcat路径
#CATALINA_OPTS=”-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs”
CLASSPATH=\                                //一定要注释此行哦
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar
case “$1” in
  start)
    #
    # Start Tomcat
    #
    $DAEMON_HOME/bin/jsvc \               //此处必须需要修改
    -user $TOMCAT_USER \
    -home $JAVA_HOME \
    -Dcatalina.home=$CATALINA_HOME \
    -Dcatalina.base=$CATALINA_BASE \
    -Djava.io.tmpdir=$TMP_DIR \
    -wait 10 \
    -pidfile $PID_FILE \
    -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 \
    exit $?
    ;;
  stop)
    #
    # Stop Tomcat
    #
    $DAEMON_HOME/bin/jsvc \               //此处必须需要修改
    -stop \
    -pidfile $PID_FILE \
    org.apache.catalina.startup.Bootstrap
    exit $?
    ;;
  *)
    echo “Usage tomcat.sh start/stop”
    exit 1;;
esac
四:添加到系统启动服务
chkconfig –add tomcat
如果报错:
service tomcat does not support chkconfig
的错误提示,则须在脚本的最前面加上一下3句
   #!/bin/bash
   # chkconfig: 2345 10 90
   # description: Starts and Stops the Tomcat daemon.
这样就可以使用service tomcat start|stop   添加到系统启动即可。

注意:

如果linux配置的第一个nameserver存在异常,在关闭tomcat的时候会报错:java.net.ConnectException: Connection refused造成关闭异常,查看端口状态

202.106.199.37:8009 SYN_SENT 3826/jsvc.exec。这个超时造成关闭异常,只能超时报错后关闭,可能是网通的dns服务器不能使用。

此条目发表在Tomcat分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注