centos添加tomcat到service系统服务

  1.  进入 /etc/init.d,创建tomcatwen文件

cd /etc/init.d

cat>tomcat

复制内容:
 

#!/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=/usr/local/java/jdk1.8.0_201

export JAVA_HOME  

CATALANA_HOME=/usr/local/tomcat/apache-tomcat-8.5.38

export CATALINA_HOME

tomcat_port=80

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


保存退出

ESC - :wq

 

.添加权限

chmod 755 tomcat

添加开机启动

chkconfig --add tomcat

查看是否添加tomcat系统服务

chkconfig --list

启动tomcat服务

sudo service tomcat start

查看是否启动成功

service tomcat status

参考文档;

https://blog.csdn.net/qq_33668603/article/details/81587916

你可能感兴趣的:(日常程序)