要是tomcat作为一个守护程序运行,需要用到jsvc工具。(这个TOMCAT已经自带了)
1、准备工作
Tomcat安装后,如果让Tomcat用root身份启动,Tomcat将会拥用root权限,这将可能给系统带来安全隐患,黑客可能利用这个来攻击我们的系统,所以我们得添加一个独立运行此服务的用户,用下面的命令:
shell>useradd -d /usr/local/tomcat/apache-tomcat-6.0.20 -s /usr/sbin/nologin tomcat
以上命令为系统增加一个tomcat用户,并且设置为nologin
设置用户tomcat6对tomcat的权限
shell>chown -R tomcat:tomcat /usr/local/tomcat/apache-tomcat-6.0.20
2、安装jsvc
shell>cd /usr/local/tomcat/apache-tomcat-6.0.20
shell>tar xvfz jsvc.tar.gz
shell>cd jsvc-src
shell>sh support/buildconf.sh
如果成功,命令终端打印出下面report:
support/buildconf.sh: configure script generated successfully
为configure添加执行权限
shell>chmod 755 configure
shell>./configure --with-java=/usr/lib/j2sdk1.6-sun (JDK安装的位置)
shell>make
cp jsvc ../(copy到上级目录,既$CATALINA_HOME/bin下)
cd ..
($CATALINA_HOME/bin)
jsvc -cp ./bin/bootstrap.jar -outfile ../logs/catalina.out -errfile ../logs/catalina.err org.apache.catalina.startup.Bootstrap
3、设置启动脚本
接下来,就是把jsvc中带有的一个tomcat5.sh的一个模板复制到/etc/init.d目录下,然后根据自己的情况,进行修改。
shell>cp /$CATALINA_HOME/bin/jsvc-src/native/Tomcat5.sh /etc/init.d/tomcat
上面的命令是移动文件tomcat5.sh到/etc/init.d/里面并改名为tomcat
4.添加服务
1>cd /etc/init.d/
2>修改/etc/init.d/tomcat头部如下
#!/bin/sh
#
#Startup Script for Tomcat6
#
#chkconfig:345 88 14
#description:Tomcat Daemon
#processname:jsvc
#pidfile:/var/run/jsvc.pid
3>
shell>chkconfig --add tomcat (如果这个命令成功,将会在/etc/rc5.d/S**tomcat文件 , 删除为 chkconfig --del tomcat )
shell>chkconfig --level 345 tomcat on
==================================================================================
作为参考(/etc/init.d/tomcat):
#!/bin/sh
#
# Startup Script for Tomcat5
#
# chkconfig: 345 88 14
# description: Tomcat Daemon
# processname: jsvc
# pidfile: /var/run/jsvc.pid
# config:
#
prog=tomcat6
JAVA_HOME=/usr/java/jdk1.6.0_15
CATALINA_HOME=/usr/local/tomcat/apache-tomcat-6.0.20
DAEMON_HOME=/usr/local/tomcat/apache-tomcat-6.0.20/bin
export JAVA_HOME
export CATALINA_HOME
export DAEMON_HOME
TOMCAT_USER=tomcat
# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/jsvc.pid
CATALINA_BASE=$CATALINA_HOME
#CATALINA_OPTS="-Djava.library.path=/home/jfclere/jakarta-tomcat-connectors/jni/native/.libs"
CATALINA_OPTS=
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/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/jsvc /
-stop /
-pidfile $PID_FILE /
org.apache.catalina.startup.Bootstrap
exit $?
;;
*)
echo "Usage tomcat.sh start/stop"
exit 1;;
esac
启动 service tomcat start
停止 service tomcat stop