当前位置: > Linux服务器 > Tomcat >

tomcat自启动脚本

时间:2014-12-12 01:39来源:51cto.com 作者:IT网
 之前给公司搭建的tomcat环境(centos安装配置JDK1.7与Tomcat7),开发人员说每次启动tomcat不方便,每次要进到/usr/local/apache-tomcat-7.0.26/bin下面去执行,很浪费时间,要求简便化.好吧,有要求就要改进,下面来看看我找的自启动脚本.
  系统:centos 5.x
  环境:apache-tomcat-7.0.26 jdk-7u3
脚本内容:
cat /etc/init.d/tomcat
#!/bin/bash
# chkconfig: 2345 10 90
# description: Start and Stop the Tomcat daemon.

TOMCAT_HOME=/usr/local/tomcat
TOMCAT_START=$TOMCAT_HOME/bin/startup.sh
TOMCAT_STOP=$TOMCAT_HOME/bin/shutdown.sh

# necessary environment variables export
CATALINA_HOME=$TOMCAT_HOME
export JAVA_HOME=/usr/java/jdk1.7.0_03

# source function library.
. /etc/rc.d/init.d/functions

# source networking configuration.
. /etc/sysconfig/network

# check that networking is up.
[ "${NETWORKING}" = "no" ] && exit 0

# check for tomcat script
if [ ! -f $TOMCAT_HOME/bin/catalina.sh ]
then echo "Tomcat not valilable..."
exit
fi

start(){
    echo -n "Starting Tomcat: "
    daemon $TOMCAT_START
    echo
    touch /var/lock/subsys/tomcat
}

stop(){
    echo -n $"Shutting down Tomcat: "
    daemon $TOMCAT_STOP
    rm -f /var/lock/subsys/tomcat.pid echo

}

restart(){
    stop
    start
}

status(){
    ps ax --width=1000 | grep "[o]rg.apache.catalina.startup.Bootstrap start" | 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 "tomcat ( pid "
        ps ax --width=1000 | grep "org.apache.catalina.startup.Bootstrap start" | awk '{printf $1 " "}'
        echo -n ") is running..."
        echo
    else
        echo "Tomcat is stopped"
    fi
}

case "$1" in
    start)
        start ;;
    stop)
        stop ;;
    restart)
        stop
        sleep 3
        start ;;
    status)
        status ;;
    *)
    echo "Usage: tomcatd {start|stop|restart|status}"
    exit 1
esac
exit 0


保存后,进到/usr/local/下,执行:
ln -s apache-tomcat-7.0.26/ tomcat

这样才符合我脚本里的路径:/usr/local/tomcat
然后你们自己确认下jdk的路径,我的是/usr/java/jdk1.7.0_03

然后给权限,做自启动:
chmod +x /etc/init.d/tomcat
chkconfig --add tomcat
chkconfig tomcat on

最后验证下脚本是否可以启动和停止tomcat
service tomcat stop
service tomcat start

如果没有问题就可以不管了,有问题的话就自己检查下jdk和tomcat的路径是否正确.

本文脚本来自:http://ontheway2015.blog.51cto.com/6159377/1243028
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容