Linux下设置Tomcat开机自启动

--未验证

第一步:在/etc/init.d下新建一个文件tomcat(需要root操作权限)

vi /etc/init.d/tomcat

然后点击"i"写下如下代码,tomcat自启动脚本

(注:需要修改以下3个部分的路径“export JRE_HOME=/usr/jdk1.8.0_131”、“export CATALINA_HOME=/usr/tomcat6”、“export CATALINA_BASE=/usr/tomcat6”)

#!/bin/sh
# chkconfig: 345 99 10
# description: Auto-starts tomcat
# /etc/init.d/tomcat
# Tomcat auto-start
# Source function library.
#. /etc/init.d/functions
# source networking configuration.
#. /etc/sysconfig/network
RETVAL=0
export JRE_HOME=/usr/jdk1.8.0_131
export CATALINA_HOME=/usr/tomcat6
export CATALINA_BASE=/usr/tomcat6
start()
{
        if [ -f $CATALINA_HOME/bin/startup.sh ];
          then
            echo $"Starting Tomcat"
                $CATALINA_HOME/bin/startup.sh
            RETVAL=$?
            echo " OK"
            return $RETVAL
        fi
}
stop()
{
        if [ -f $CATALINA_HOME/bin/shutdown.sh ];
          then
            echo $"Stopping Tomcat"
                $CATALINA_HOME/bin/shutdown.sh
            RETVAL=$?
            sleep 1
            ps -fwwu tomcat | grep apache-tomcat|grep -v grep | grep -v PID | awk '{print $2}'|xargs kill -9
            echo " OK"
            # [ $RETVAL -eq 0 ] && rm -f /var/lock/...
            return $RETVAL
        fi
}

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

exit $RETVAL

最后点击"Esc",输入“:wq”

第二步、设置权限

chmod +x /etc/init.d/tomcat

第三步、设定开机启动服务

sudo chkconfig tomcat on

第四步、验证

重启服务器,然后访问

你可能感兴趣的:(Linux下设置Tomcat开机自启动)