Linux设置tomcat为系统服务

对于需要在Linux系统上部署JSP,Servlet的Web应用服务器的人来说,将Tomcat安装为系统的服务(Service)是一个很常见的任务,这篇文章给大家一个详细的安装步骤,我们选择的系统和软件版本为:CentOS5.1(RHEL5也是一样的),Tomcat6.0.16。具体安装步骤如下:

一,到Apache下载Tomcat6.0.16(http://apache.mirror.phpchina.com/tomcat/tomcat-6/v6.0.16/bin/apache-tomcat-6.0.16.tar.gz)。

二,解压缩Tomcat文件到/opt目录下(需要root用户权限)

#tarxvzfapache-tomcat-6.0.16.tar.gz

确认你的目录为/opt/apache-tomcat-6.0.16

三,测试你的tomcat是不是能够正常运行(确保JAVA_HOME环境变量设置过)

启动

#/opt/apache-tomcat-6.0.16/start.sh

停止

#/opt/apache-tomcat-6.0.16/shutdown.sh

四,将Tomcat部署为系统的服务(以jsvc方式运行)

1,解压缩jsvc包(/opt/apache-tomcat-6.0.16/bin/jsvc.tar.gz)

#tarxvzfjsvc.tar.gz

2,编译jsvc,并测试(假设jdk的安装路径:/usr/java/jdk1.6.0_06)

#cd/opt/apache-tomcat-6.0.16/bin/jsvc-src

#chmod+xconfigure

#.configure--with-java=/usr/java/jdk1.6.0_06

#.make

#cpjsvc../

#cp/opt/apache-tomcat-6.0.16/bin/jsvc-src/native/Tomcat5.sh/etc/init.d/tomcat

编辑刚copy的tomcat文件

#vi/etc/init.d/tomcat

编辑完成后的内容如下:

#!/bin/sh

#

#StartupScriptforTomcat6

#

#chkconfig:-8814

#description:TomcatDaemon

#processname:jsvc

#pidfile:/var/run/jsvc.pid

#config:

#

#Sourcefunctionlibrary.

./etc/rc.d/init.d/functions

#

#Adaptthefollowinglinestoyourconfiguration

JAVA_HOME=/usr/java/jdk1.6.0_06

CATALINA_HOME=/opt/apache-tomcat-6.0.16

DAEMON_HOME=$CATALINA_HOME/bin

TOMCAT_USER=tomcat

#formultiinstancesadaptthoselines.

TMP_DIR=$CATALINA_HOME/temp

PID_FILE=/var/run/jsvc.pid

CATALINA_BASE=$CATALINA_HOME

CATALINA_OPTS=

CLASSPATH=\

$JAVA_HOME/lib/tools.jar:\

$CATALINA_HOME/bin/commons-daemon.jar:\

$CATALINA_HOME/bin/bootstrap.jar

start(){

echostartingtomcat...

$DAEMON_HOME/jsvc\

-user$TOMCAT_USER\

-home$JAVA_HOME\

-Dcatalina.home=$CATALINA_HOME\

-Dcatalina.base=$CATALINA_BASE\

-Djava.io.tmpdir=$TMP_DIR\

-wait10\

-pidfile$PID_FILE\

-outfile$CATALINA_HOME/logs/catalina.out\

-errfile'&1'$CATALINA_OPTS\

-cp$CLASSPATHorg.apache.catalina.startup.Bootstrap

}

stop(){

echostoppingtomcat...

$DAEMON_HOME/jsvc\

-stop\

-pidfile$PID_FILE\

org.apache.catalina.startup.Bootstrap

}

status(){

psax--width=1000|grep"[o]rg.apache.catalina.startup.Bootstrap"|awk'{printf$1""}'|wc|awk'{print$2}'>/tmp/tomcat_process_count.txt

readline</tmp/tomcat_process_count.txt

if[$line-gt0];then

echo-n"tomcat(pid"

psax--width=1000|grep"[o]rg.apache.catalina.startup.Bootstrap"|awk'{printf$1""}'

echo")isrunning..."

else

echo"Tomcatisstopped"

fi

}

case"$1"in

start)

#StartTomcat

start

exit$?

;;

stop)

#StopTomcat

stop

exit$?

;;

restart)

stop

sleep3

start

exit$?

;;

status)

status

exit$?

;;

*)

echo"Usage:tomcat{start|stop|restart|status}"

exit1

;;

esac

添加执行用户

#useraddtomcat

确保tomcat用户能够访问tomcat的目录

#chown-Rtomcat/opt/apache-tomcat-6.0.16

#chgrp-Rtomcat/opt/apache-tomcat-6.0.16

#chmod+x/etc/init.d/tomcat

测试是否能启动tomcat

#/etc/init.d/tomcatstart

#/etc/init.d/tomcatstop

将tomcat部署成为自动运行的服务

#chkconfigtomcaton

启动服务

#servicetomcatstart

查询服务状态

#servicetomcatstatus

停止服务

#servicetomcatstop

相关推荐