写一个java服务的启停脚本

#!/bin/bash

# 检查参数数量
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 {start|stop|restart} app_name"
    exit 1
fi

action=$1
app_name=$2

# 检查ACTION是否为start或stop
#if [ "${action}" != "start" ] && [ "${action}" != "stop" ] && [ "${action}" != "restart" ]; then
#    echo "命令错误: action must be either 'start' or 'stop' or 'restart'"
#    exit 1
#fi

# 定义数组
app_list=("ebc-gateway" "ebc-provider-common" "ebc-provider-user" "ebc-rocket-consumer" "ebc-web-consumer" "ebc-wxchat-consumer")

exists=false

function start(){
    app=$1
    for element in "${app_list[@]}"; do
       if [ "${app}" == "${element}" ]; then
           exists=true
           break
       fi
    done
    if [ "${exists}" == false ]; then
       echo "${app}服务不存在"
       exit 1
    fi

    echo "启动的服务是:${app}"
    cd /dev-ebc/${app}
    latest_version=$(ls | tail -n 1)
    cd ${latest_version}
    #nohup java -jar ${app}-1.0.0.jar > nohup.log  2>&1 &
    echo "当前目录是:$(pwd),文件列表:$(ls *.jar)"
    if ps axu | grep ${app} | grep -v grep  | grep -v bash 
      then
        echo -e "\033[32m${app}启动成功!!\033[0m"
      else
        echo -e "\033[31m${app}启动失败!!\033[0m"
    fi
}

function stop(){
    app=$1
    for element in "${app_list[@]}"; do
      if [ "${app}" == "${element}" ]; then
         exists=true
         break 
      fi
    done
    if [ "${exists}" == false ]; then
       echo "${app}服务不存在"
       exit 1
    fi

    app_ps_id=$(ps aux | grep ${app} | grep -vE  'grep|bash' | awk '{print $2}') 
    echo "${app}当前进程ID:${app_ps_id}"
    #kill ${app_ps_id}
    if  ps axu | grep ${app} | grep -vE 'bash|grep' | awk '{print $2}' > /dev/null
       then
          echo "停服务失败"
       else
          echo "停服务成功"
    fi
}

case ${action} in
    start) 
         #echo "执行start函数,start ${app_name}"
         start ${app_name}
    ;;
    stop) 
         #echo "执行stop函数,stop ${app_name}"
         stop ${app_name}
    ;;
    restart) 
         #echo "重启${app_name}"
         stop  ${app_name}
         start ${app_name}
    ;;
    *)
         echo "命令错误: action must be either 'start' or 'stop' or 'restart'"
esac

  

posted @ 2024-12-23 10:37  羊脂玉净瓶  阅读(13)  评论(0)    收藏  举报