tomcat在centos6+上的自启动脚本
#!/bin/bash
#
# tomcat startup script for the Tomcat server
#
# chkconfig: 345 80 20
# description: start the tomcat deamon
#
# Source function library
. /etc/rc.d/init.d/functions
prog=tomcat
JAVA_HOME=/opt/jvm/jdk8/
export JAVA_HOME
CATALANA_HOME=/opt/tomcat/tomcat7
export CATALINA_HOME
tomcat_port=8080
case "$1" in
start)
tomcat_PID=`netstat -nptl | grep $tomcat_port | awk '{print $7}'`
if [ "$tomcat_PID" != "" ]
then
echo "Tomcat had started"
echo "Tomcat_PID="${tomcat_PID}
echo "if you want to restart Tomcat,please excute restart command!"
else
echo "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
fi
;;
stop)
tomcat_PID=`netstat -nptl | grep $tomcat_port | awk '{print $7}'`
if [ "$tomcat_PID" != "" ]
then
echo "Stopping Tomcat..."
$CATALANA_HOME/bin/shutdown.sh
else
echo "Tomcat hadn't started,please excute start command!"
fi
;;
restart)
#获取tomcat的进程ID
#TomcatID=$(ps -ef |grep tomcat |grep -w $CATALANA_HOME|grep -v 'grep'|awk '{print $2}')
tomcat_PID=`netstat -nptl | grep $tomcat_port | awk '{print $7}'`
#echo "tomcat_PID="$tomcat_PID
if [ "$tomcat_PID" != "" ]
then
echo "Tomcat had started,we will stopping Tomcat and Starting Tomcat next 2 second..."
echo "Stopping Tomcat..."
$CATALANA_HOME/bin/shutdown.sh
echo ""
echo "...............waiting 2 second.................."
echo ""
sleep 2
echo -n "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
else
echo "Tomcat hadn't starting"
echo -n "Starting Tomcat..."
$CATALANA_HOME/bin/startup.sh
fi
;;
*)
echo "Usage: $prog {start|stop|restart}"
;;
esac
exit 0
注册服务:
# chmod 700 tomcat.sh
# cp tomcat.sh /etc/init.d/tomcatd
# chkconfig --add tomcatd
# chkconfig --list
删除服务:
# chkconfig --del tomcatd
注册完服务后就可以使用service tomcatd start启动Tomcat。
服务器自启动时,服务自动启动。