sleep 3 if ps -p "$NEW_PID" > /dev/null 2>&1; then echo "Application started successfully, PID=$NEW_PID" else echo "Failed to start application, please check log: $LOG_FILE" rm -f "$PID_FILE" exit 1 fi } # ===================== 停止 ===================== stop() { local PID=$(get_pid) if [ -z "$PID" ]; then echo "Application is not running" exit 0 fi
echo "Stopping application (PID=$PID), waiting up to $STOP_TIMEOUT seconds..." kill "$PID"
local count=0 while ps -p "$PID" > /dev/null 2>&1 && [ $count -lt $STOP_TIMEOUT ]; do sleep 1 count=$((count + 1)) echo -n "." done echo ""
if ps -p "$PID" > /dev/null 2>&1; then echo "Stop timeout, killing process (PID=$PID)" kill -9 "$PID" sleep 2 fi
rm -f "$PID_FILE" echo "Application stopped" } # ===================== 重启 ===================== restart() { echo "Restarting application..." stop sleep 2 start } # ===================== 状态 ===================== status() { local PID=$(get_pid) if [ -n "$PID" ]; then echo "Application is running, PID=$PID" exit 0 else echo "Application is not running" exit 1 fi } # ===================== 主逻辑 ===================== case "$1" in start) start ;; stop) stop ;; restart) restart ;; status) status ;; *) echo "Usage: $0 {start|stop|restart|status}" exit 1 ;; esac