使用init在一个服务器中启动多个tomcat实例。d脚本

时间:2023-01-18 19:08:54

I'm trying to configure tomcat init.d start script to work with multiple instances (at this time 2 instances)

我正在尝试配置tomcat init。d启动脚本以处理多个实例(此时是两个实例)

I'm following below sample script to create init.d script

我按照下面的示例脚本创建init。d脚本

#!/bin/bash
#
# tomcat     This shell script takes care of starting and stopping Tomcat
#
# chkconfig: - 80 20
#
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start:
# Default-Stop:
# Short-Description: start and stop tomcat
### END INIT INFO

TOMCAT_USER=root
TOMCAT_HOME="/opt/tomcat7/node1"
SHUTDOWN_WAIT=45

tomcat_pid() {
    echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep -v grep | awk '{ print $2 }'`
}

start() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Tomcat is already running (pid: $pid)"
    else
        # Start tomcat
        echo "Starting tomcat service"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/startup.sh" $TOMCAT_USER
    fi
    return 0
}

stop() {
    pid=$(tomcat_pid)
    if [ -n "$pid" ]
    then
        echo "Stoping Tomcat"
        /bin/su - -c "cd $TOMCAT_HOME/bin && $TOMCAT_HOME/bin/shutdown.sh" $TOMCAT_USER

    let kwait=$SHUTDOWN_WAIT
    count=0
    count_by=5
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
    do
        echo "Waiting for processes to exit. Timeout before we kill the pid: ${count}/${kwait}"
        sleep $count_by
        let count=$count+$count_by;
    done

    if [ $count -gt $kwait ]; then
        echo "Killing processes which didn't stop after $SHUTDOWN_WAIT seconds"
        kill -9 $pid
    fi
    else
        echo "Tomcat is not running"
    fi

    return 0
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
       pid=$(tomcat_pid)
        if [ -n "$pid" ]
        then
           echo "Tomcat is running with pid: $pid"
        else
           echo "Tomcat is not running"
        fi
        ;;
esac

exit 0

problem is tomcat_pid() method returns process ids of all tomcat instances, because of that, the second instance cannot be started. Is there a better method to handle this?

问题是tomcat_pid()方法返回所有tomcat实例的进程id,因此无法启动第二个实例。有更好的方法来处理这个问题吗?

1 个解决方案

#1


0  

found a workaround, but expecting better solution

找到一个变通的办法,但期待更好的解决方案

using netstat we can find process id via running port number

使用netstat我们可以通过运行端口号找到进程id。

echo `netstat -tlnp | awk '/:80  */ {split($NF,a,"/"); print a[1]}'`

So i modified the function tomcat_pid() as below

因此我修改了函数tomcat_pid()如下所示

tomcat_pid() {
    echo `netstat -tlnp | awk '/:<port>  */ {split($NF,a,"/"); print a[1]}'`
}

#1


0  

found a workaround, but expecting better solution

找到一个变通的办法,但期待更好的解决方案

using netstat we can find process id via running port number

使用netstat我们可以通过运行端口号找到进程id。

echo `netstat -tlnp | awk '/:80  */ {split($NF,a,"/"); print a[1]}'`

So i modified the function tomcat_pid() as below

因此我修改了函数tomcat_pid()如下所示

tomcat_pid() {
    echo `netstat -tlnp | awk '/:<port>  */ {split($NF,a,"/"); print a[1]}'`
}