#!/bin/sh # ./cds.sh start 启动 stop 停止 restart 重启 status 状态 AppName=mps-server.jar ServerName=mps-server # 应用端口配置/需要与maps-server启动端口保持一致 APP_PORT=10800 # JVM参数 JVM_OPTS="-Dname=$AppName -Duser.timezone=Asia/Shanghai -Xms2048m -Xmx4096m -XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=2048m -XX:+HeapDumpOnOutOfMemoryError -XX:+UseZGC -Djasypt.encryptor.password=66668888" APP_HOME=$(pwd) LOG_PATH=$APP_HOME/logs/$ServerName.log # 健康检查超时时间/秒 HEALTH_CHECK_TIMEOUT=60 echo_with_timestamp() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $@" } if [ "$1" = "" ]; then echo -e "\033[0;31m 未输入操作名 \033[0m \033[0;34m {start|stop|restart|status} \033[0m" exit 1 fi if [ "$AppName" = "" ]; then echo -e "\033[0;31m 未输入应用名 \033[0m" exit 1 fi function wait_for_health_check() { local timeout=$1 local pid=$2 local elapsed=0 local interval=2 while [ $elapsed -lt $timeout ]; do # 检查日志中的启动完成消息,暂定使用检查服务端口的方式 #if grep -q "mps started successfully" "$LOG_PATH" 2>/dev/null; then # echo_with_timestamp ">>>>>>>> Check log info pass..." # return 0 #fi # 检查服务端口启动成功/端口启动即表示服务启动成功 if netstat -tln | grep -q ":$APP_PORT "; then echo_with_timestamp ">>>>>>>> Check app server port start suc..." return 0 fi # app pid已经关闭,退出health eheck if ! ps -p $pid > /dev/null; then echo_with_timestamp ">>>>>>>> Check app pid existing fail..." return 1 fi echo_with_timestamp ">>>>>>>> Waiting for $interval second to recheck..." sleep $interval elapsed=$((elapsed + interval)) done return 1 } function start() { echo "----------------------------------------------------------" PID=$(ps -ef | grep java | grep "$AppName" | grep -v grep | awk '{print $2}') if [ -n "$PID" ]; then echo_with_timestamp ">>>> $ServerName is already running..." echo "----------------------------------------------------------" return 0 fi # 启动前确保日志目录存在 mkdir -p "$APP_HOME/logs" # 使用nohup启动应用并记录PID echo_with_timestamp ">>>> Ready to starting $ServerName on port $APP_PORT..." nohup java $JVM_OPTS -jar "$AppName" >> "$LOG_PATH" 2>&1 & local new_pid=$! echo_with_timestamp ">>>> Started $ServerName with PID: $new_pid" # 等待健康检查 echo_with_timestamp ">>>>>> Starting health check..." if wait_for_health_check $HEALTH_CHECK_TIMEOUT $new_pid; then echo_with_timestamp ">>>> Start $ServerName success..." else echo_with_timestamp ">>>>>> Start $ServerName failed or health check timeout..." # 尝试获取退出状态 ----- 当健康检查超时而server启动成功时会卡住,不使用 #if wait $new_pid 2>/dev/null; then # local exit_status=$? # echo_with_timestamp ">>>> Application exited with status: $exit_status" #fi echo "----------------------------------------------------------" return 1 fi echo "----------------------------------------------------------" } function stop() { echo "----------------------------------------------------------" echo_with_timestamp ">>>> Ready to stopping $ServerName..." PID="" query(){ PID=$(ps -ef | grep java | grep "$AppName" | grep -v grep | awk '{print $2}') } query if [ -n "$PID" ]; then echo_with_timestamp ">>>>>> Found running process with PID: $PID" kill -TERM "$PID" echo_with_timestamp ">>>>>> $ServerName (pid:$PID) exiting..." while [ -n "$PID" ] do sleep 1 query done echo_with_timestamp ">>>>>> $ServerName exited." else echo_with_timestamp ">>>> $ServerName already stopped." fi echo_with_timestamp ">>>> Stop $ServerName success..." echo "----------------------------------------------------------" } function restart() { echo_with_timestamp ">> Ready to restarting $ServerName..." stop sleep 2 start } function status() { echo "----------------------------------------------------------" PID=$(ps -ef | grep java | grep "$AppName" | grep -v grep | awk '{print $2}') if [ -n "$PID" ]; then echo_with_timestamp ">> $ServerName is running (PID: $PID)" local port_listening=$(netstat -tln | grep ":$APP_PORT " | wc -l) if [ "$port_listening" -gt 0 ]; then echo_with_timestamp ">> Application is listening on port $APP_PORT" #需要root用户或其他用户增加netstat权限 local port_info=$(netstat -tlnp | grep ":$APP_PORT " | grep java) if [ -n "$port_info" ]; then echo_with_timestamp ">> Port details: $port_info" fi if command -v nc >/dev/null 2>&1; then if nc -z localhost $APP_PORT; then echo_with_timestamp ">> Port $APP_PORT is accessible" else echo_with_timestamp ">> Warning: Port $APP_PORT is listening but not accessible" fi fi else echo_with_timestamp ">> Warning: Application is running but not listening on port $APP_PORT" fi # 运行时间 local uptime=$(ps -o etime= -p "$PID" 2>/dev/null | xargs) if [ -n "$uptime" ]; then echo_with_timestamp ">> Process uptime: $uptime" fi # 内存使用 local memory_usage=$(ps -o rss= -p "$PID" 2>/dev/null | xargs) if [ -n "$memory_usage" ]; then memory_usage=$((memory_usage / 1024)) echo_with_timestamp ">> Memory usage: ${memory_usage}MB" fi else echo_with_timestamp ">> $ServerName is not running" # 端口被占用 local port_used_by=$(netstat -tlnp | grep ":$APP_PORT " | awk '{print $7}' | cut -d'/' -f1) if [ -n "$port_used_by" ]; then echo_with_timestamp ">> Warning: Port $APP_PORT is being used by another process (PID: $port_used_by)" fi fi echo "----------------------------------------------------------" } function show_config() { echo "----------------------------------------------------------" echo_with_timestamp ">> Application Configuration:" echo_with_timestamp ">>>> Name: $ServerName" echo_with_timestamp ">>>> Port: $APP_PORT" echo_with_timestamp ">>>> Home: $APP_HOME" echo_with_timestamp ">>>> Log : $LOG_PATH" echo "----------------------------------------------------------" } case $1 in start) start ;; stop) stop ;; restart) restart ;; status) status ;; config) show_config ;; *) echo_with_timestamp "Usage: $0 {start|stop|restart|status|config}" exit 1 ;; esac