CentOS/SUSE/Linux 下Tomcat 自动启动脚本

首先为Tomcat编辑一段启动脚本,并置于/etc/rc.d/init.d目录下,让Tomcat和httpd一样作为一项系统服务,并随系统自动启动。(注意配置红色字体的JDK和Tomcat的路径)

# vi /etc/rc.d/init.d/tomcat

#!/bin/bash
#
# Startup script for the tomcat
#
# chkconfig: 2345 90 10
# description: The Tomcat Java Application Server
#
# Source function library.
. /etc/rc.d/init.d/functions

JAVA_HOME=" /usr/local/jdk1.6.0_33"
TOMCAT_HOME=" /usr/local/apache-tomcat-7.0.29"

export JAVA_HOME="${JAVA_HOME}"

PID_CMD="ps aux | grep 'org.apache.catalina.startup.Bootstrap' | grep -v 'grep' | grep -v '0\.0' | awk '{print \$2}'"

start(){
if [ -z `eval ${PID_CMD}` ]; then
echo "Starting tomcat..."
${TOMCAT_HOME}/bin/startup.sh
touch /var/lock/subsys/tomcat
else
echo "Tomcat is already running."
fi
}

stop(){
if [ ! -z `eval ${PID_CMD}` ]; then
echo "Shutting down tomcat..."
${TOMCAT_HOME}/bin/shutdown.sh
until [ -z `eval ${PID_CMD}` ]; do :; done
rm -f /var/lock/subsys/tomcat
else
echo "Tomcat is not running."
fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
${TOMCAT_HOME}/bin/catalina.sh version
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
esac

exit 0

[root@sample ~]# chmod 755 /etc/rc.d/init.d/tomcat  ← 将启动脚本属性设置为755(使之可执行)
[root@sample ~]# chkconfig --add tomcat  ← 将Tomcat加入到chkconfig中
[root@sample ~]# chkconfig tomcat on   ← 将Tomcat设置为自启动
[root@sample ~]# chkconfig --list tomcat  ← 确认Tomcat服务状态
tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 确认2~5为on的状态就OK
[root@sample ~]# /etc/rc.d/init.d/tomcat start  ← 启动Tomcat

以后可以用以下命令来启动/关闭/重启Tomcat
service tomcat start
service tomcat stop
service tomcat restart

你可能感兴趣的:(CentOS/SUSE/Linux 下Tomcat 自动启动脚本)