#!/bin/bash

# 设置Tomcat路径
TOMCAT_HOME="/www/tomcat-cpms"

# 定义Tomcat启动命令
START_COMMAND="${TOMCAT_HOME}/bin/startup.sh"

# 定义Tomcat停止命令
STOP_COMMAND="${TOMCAT_HOME}/bin/shutdown.sh"

# 检查Tomcat进程是否在运行
is_tomcat_running() {
    ps_output=$(ps aux | grep "${TOMCAT_HOME}" | grep -v "grep")
    if [ -n "$ps_output" ]; then
        return 0  # Tomcat进程正在运行
    else
        return 1  # Tomcat进程未运行
    fi
}

# 启动Tomcat
start_tomcat() {
    if ! is_tomcat_running; then
        echo "Starting Tomcat..."
        ${START_COMMAND}
        echo "Tomcat started."
    else
        echo "Tomcat is already running."
    fi
}

# 停止Tomcat
stop_tomcat() {
    if is_tomcat_running; then
        echo "Stopping Tomcat..."
        ${STOP_COMMAND}
        echo "Tomcat stopped."
    else
        echo "Tomcat is not running."
    fi
}

# 主循环
while true; do
    # 检查Tomcat进程状态
    if ! is_tomcat_running; then
        echo "Tomcat process is not running. Starting..."
        start_tomcat
    fi

    # 每隔100秒检查一次Tomcat进程
    sleep 100
done

while true; do
  sleep 1
done
