| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- #!/usr/bin/env bash
- set -euo pipefail
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- RUN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
- RUNTIME_DIR="${RUN_ROOT}/runtime/full-local"
- MYSQL_HOME="${ZQ_MYSQL_HOME:-/usr/local/mysql-5.7.25-macos10.14-x86_64}"
- MYSQL_PORT="${ZQ_LOCAL_MYSQL_PORT:-3318}"
- MYSQL_SOCKET="${RUNTIME_DIR}/mysql/mysql.sock"
- APP_PORT="${ZQ_FULL_PORT:-4188}"
- REDIS_PORT="${ZQ_LOCAL_REDIS_PORT:-6389}"
- REDIS_PASSWORD="${ZQ_REDIS_PASSWORD:-zq_local_redis_2026}"
- REDIS_PID="${RUNTIME_DIR}/redis/redis.pid"
- TOMCAT_HOME="${ZQ_TOMCAT_HOME:-${RUN_ROOT}/tools/apache-tomcat-${ZQ_TOMCAT_VERSION:-8.5.100}}"
- TOMCAT_BASE="${RUNTIME_DIR}/tomcat-base"
- TOMCAT_PID="${RUNTIME_DIR}/zq-tomcat.pid"
- MYSQL_LAUNCHD_LABEL="com.feifan.zq.full.mysql"
- TOMCAT_LAUNCHD_LABEL="com.feifan.zq.full.tomcat"
- log() {
- printf '[ZQ本机完整后台] %s\n' "$*"
- }
- is_running_pid() {
- local pid="${1:-}"
- [[ -n "${pid}" ]] && kill -0 "${pid}" 2>/dev/null
- }
- launchctl_domain() {
- printf 'gui/%s' "$(id -u)"
- }
- launchctl_available() {
- command -v launchctl >/dev/null 2>&1 && launchctl print "$(launchctl_domain)" >/dev/null 2>&1
- }
- launchctl_bootout_label() {
- local label="${1:-}"
- [[ -n "${label}" ]] || return 0
- launchctl_available || return 0
- launchctl bootout "$(launchctl_domain)/${label}" >/dev/null 2>&1 || true
- }
- find_redis_cli() {
- if [[ -n "${ZQ_REDIS_CLI:-}" && -x "${ZQ_REDIS_CLI}" ]]; then
- printf '%s' "${ZQ_REDIS_CLI}"
- elif command -v redis-cli >/dev/null 2>&1; then
- command -v redis-cli
- elif [[ -x "/opt/homebrew/bin/redis-cli" ]]; then
- printf '%s' "/opt/homebrew/bin/redis-cli"
- elif [[ -x "/usr/local/bin/redis-cli" ]]; then
- printf '%s' "/usr/local/bin/redis-cli"
- fi
- }
- redis_ping() {
- local cli
- cli="$(find_redis_cli || true)"
- [[ -n "${cli}" ]] || return 1
- "${cli}" -h 127.0.0.1 -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" --no-auth-warning ping 2>/dev/null | grep -q '^PONG$'
- }
- mysql_port_pids() {
- if command -v lsof >/dev/null 2>&1; then
- lsof -tiTCP:"${MYSQL_PORT}" -sTCP:LISTEN 2>/dev/null || true
- fi
- }
- mysql_data_lock_pids() {
- if command -v lsof >/dev/null 2>&1 && [[ -f "${RUNTIME_DIR}/mysql/data/ibdata1" ]]; then
- lsof -t "${RUNTIME_DIR}/mysql/data/ibdata1" 2>/dev/null || true
- fi
- }
- web_port_pids() {
- if command -v lsof >/dev/null 2>&1; then
- lsof -tiTCP:"${APP_PORT}" -sTCP:LISTEN 2>/dev/null || true
- fi
- }
- redis_port_pids() {
- if command -v lsof >/dev/null 2>&1; then
- lsof -tiTCP:"${REDIS_PORT}" -sTCP:LISTEN 2>/dev/null || true
- fi
- }
- launchctl_bootout_label "${TOMCAT_LAUNCHD_LABEL}"
- if [[ -f "${TOMCAT_PID}" ]] && is_running_pid "$(cat "${TOMCAT_PID}")"; then
- export CATALINA_HOME="${TOMCAT_HOME}"
- export CATALINA_BASE="${TOMCAT_BASE}"
- export CATALINA_PID="${TOMCAT_PID}"
- "${TOMCAT_HOME}/bin/catalina.sh" stop 20 -force >/dev/null 2>&1 || true
- log "已停止 ZQ Web 服务。"
- if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
- pids="$(web_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
- kill ${pids} >/dev/null 2>&1 || true
- sleep 2
- if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
- kill -9 ${pids} >/dev/null 2>&1 || true
- fi
- log "已清理 ZQ Web 残留进程。"
- fi
- else
- if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
- pids="$(web_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
- kill ${pids} >/dev/null 2>&1 || true
- sleep 2
- if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
- kill -9 ${pids} >/dev/null 2>&1 || true
- fi
- log "已清理 ZQ Web 残留进程。"
- else
- log "ZQ Web 服务当前没有运行。"
- fi
- rm -f "${TOMCAT_PID}"
- fi
- if redis_ping; then
- "$(find_redis_cli)" -h 127.0.0.1 -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" --no-auth-warning shutdown nosave >/dev/null 2>&1 || true
- log "已停止项目专用 Redis。"
- elif [[ -f "${REDIS_PID}" ]] && is_running_pid "$(cat "${REDIS_PID}")"; then
- kill "$(cat "${REDIS_PID}")" >/dev/null 2>&1 || true
- log "已停止项目专用 Redis。"
- elif [[ -n "$(redis_port_pids | tr -d '[:space:]')" ]]; then
- pids="$(redis_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
- kill ${pids} >/dev/null 2>&1 || true
- sleep 2
- if [[ -n "$(redis_port_pids | tr -d '[:space:]')" ]]; then
- kill -9 ${pids} >/dev/null 2>&1 || true
- fi
- log "已清理项目专用 Redis 残留进程。"
- rm -f "${REDIS_PID}"
- else
- log "项目专用 Redis 当前没有运行。"
- rm -f "${REDIS_PID}"
- fi
- launchctl_bootout_label "${MYSQL_LAUNCHD_LABEL}"
- if [[ -x "${MYSQL_HOME}/bin/mysqladmin" ]]; then
- if "${MYSQL_HOME}/bin/mysqladmin" --socket="${MYSQL_SOCKET}" -uroot ping >/dev/null 2>&1; then
- "${MYSQL_HOME}/bin/mysqladmin" --socket="${MYSQL_SOCKET}" -uroot shutdown >/dev/null 2>&1 || true
- log "已停止项目专用 MySQL。"
- elif [[ -n "$(mysql_port_pids | tr -d '[:space:]')" ]]; then
- pids="$(mysql_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
- kill ${pids} >/dev/null 2>&1 || true
- sleep 2
- if [[ -n "$(mysql_port_pids | tr -d '[:space:]')" ]]; then
- kill -9 ${pids} >/dev/null 2>&1 || true
- fi
- log "已清理项目专用 MySQL 残留进程。"
- rm -f "${RUNTIME_DIR}/mysql/mysql.pid" "${MYSQL_SOCKET}" "${MYSQL_SOCKET}.lock"
- else
- log "项目专用 MySQL 当前没有运行。"
- rm -f "${RUNTIME_DIR}/mysql/mysql.pid" "${MYSQL_SOCKET}" "${MYSQL_SOCKET}.lock"
- fi
- if [[ -n "$(mysql_data_lock_pids | tr -d '[:space:]')" ]]; then
- pids="$(mysql_data_lock_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
- kill ${pids} >/dev/null 2>&1 || true
- sleep 2
- if [[ -n "$(mysql_data_lock_pids | tr -d '[:space:]')" ]]; then
- kill -9 ${pids} >/dev/null 2>&1 || true
- fi
- log "已清理项目专用 MySQL 数据文件残留占用。"
- fi
- else
- log "没有找到 MySQL 管理命令。"
- fi
- remaining=""
- if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
- remaining="${remaining} ZQ-Web(${APP_PORT})"
- fi
- if [[ -n "$(redis_port_pids | tr -d '[:space:]')" ]]; then
- remaining="${remaining} Redis(${REDIS_PORT})"
- fi
- if [[ -n "$(mysql_port_pids | tr -d '[:space:]')" || -n "$(mysql_data_lock_pids | tr -d '[:space:]')" ]]; then
- remaining="${remaining} MySQL(${MYSQL_PORT})"
- fi
- if [[ -n "${remaining}" ]]; then
- log "仍有残留进程没有退出:${remaining}。请关闭正在打开的“ZQ本机完整后台”终端窗口,或重启电脑后再双击启动。"
- exit 1
- fi
|