如果需要将tomcat作为Linux的后台程序运行,需要使用commons-daemon工程的jsvc工具,tomcat的bin目录里已经自带了这个工具的源码。
操作环境:centos 7,Tomcat7,jdk 1.7
操作步骤如下:
解压commons-daemon-native.tar.gz
tar -zxvf commons-daemon-native.tar.gz
解压后会生成commons-daemon-1.0.15-native-src目录,进入unix目录下
cd commons-daemon-1.0.15-native-src/unix
执行配置并进行编译
注意需要预先安装gcc,如果没有安装,则使用 yum install -y gcc进行安装
./configure
make
将生成的jsvc文件拷贝到Tomcat的bin目录下
在/etc/init.d中创建tomcat文件,并写入如下脚本内容
#!/bin/sh
# tomcat: Start/Stop/Restart tomcat
#
# chkconfig: 2345 85 15
# description: Apache tomcat7
# 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"/>
#
#根据实际jdk路径进行配置
JAVA_HOME=/usr/local/bin/jdk1.7.0_75
#根据实际tomcat路径进行配置
CATALINA_HOME=/usr/local/bin/apache-tomcat-7.0.69
CATALINA_BASE=$CATALINA_HOME
DAEMON_HOME=$CATALINA_HOME/bin
TOMCAT_USER=egov
# for multi instances adapt those lines.
#根据实际tomcat路径进行配置,设置为tomcat路径/temp形式
TMP_DIR=$CATALINA_HOME/temp
#根据实际tomcat路径进行配置,设置为tomcat路径/jsvc/run/jsvc.pid形式
PID_FILE=/home/egov/tomcat/jsvc/run/jsvc.pid
#. /etc/rc.d/init.d/functions
CATALINA_OPTS="$CATALINA_OPTS -server -Xms2048m -Xmx2048m -XX:PermSize=512m -XX:MaxNewSize=512m -XX:MaxPermSize=1024m"
CLASSPATH=\
$JAVA_HOME/lib/tools.jar:\
$CATALINA_HOME/bin/commons-daemon.jar:\
$CATALINA_HOME/bin/bootstrap.jar:\
$CATALINA_HOME/bin/tomcat-juli.jar
case "$1" in
start)
#
# Start Tomcat
#
$DAEMON_HOME/jsvc \
-user $TOMCAT_USER \
-home $JAVA_HOME \
-Dcatalina.home=$CATALINA_HOME \
-Dcatalina.base=$CATALINA_BASE \
-Dfile.encoding=utf-8 \
-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
(ps -e | awk '{ if($4== "jsvc") printf "%s ",$1}' | xargs kill -9)2>/dev/null
exit $?
;;
restart)
#
# Restart Tomcat
#
$0 stop
$0 start
;;
status)
#
# Restart Tomcat
#
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap" | awk '{printf $1 " "}' | wc | awk '{print $2}' > /tmp/tomcat_process_count.txt
read line < /tmp/tomcat_process_count.txt
if [ $line -gt 0 ]; then
echo -n "tomcat7 ( pid "
ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap" | awk '{printf $1 " "}'
echo ") is running "
else
echo "tomcat7 is stopped"
fi
;;
*)
echo "Usage tomcat7 start/stop/restart/status"
exit 1;;
esac
根据实际情况修改JAVA_HOME,CATALINA_HOME以及TOMCAT_USER的值
修改tomcat文件的属性
chmod 755 tomcat
添加系统服务
chkconfig --add tomcat
查看是否添加tomcat系统服务
chkconfig --list
启动tomcat服务
service tomcat start
查看是否启动成功
service tomcat status
关闭tomcat服务
service tomcat stop