stop-full-local.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. RUN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  5. RUNTIME_DIR="${RUN_ROOT}/runtime/full-local"
  6. MYSQL_HOME="${ZQ_MYSQL_HOME:-/usr/local/mysql-5.7.25-macos10.14-x86_64}"
  7. MYSQL_PORT="${ZQ_LOCAL_MYSQL_PORT:-3318}"
  8. MYSQL_SOCKET="${RUNTIME_DIR}/mysql/mysql.sock"
  9. APP_PORT="${ZQ_FULL_PORT:-4188}"
  10. REDIS_PORT="${ZQ_LOCAL_REDIS_PORT:-6389}"
  11. REDIS_PASSWORD="${ZQ_REDIS_PASSWORD:-zq_local_redis_2026}"
  12. REDIS_PID="${RUNTIME_DIR}/redis/redis.pid"
  13. TOMCAT_HOME="${ZQ_TOMCAT_HOME:-${RUN_ROOT}/tools/apache-tomcat-${ZQ_TOMCAT_VERSION:-8.5.100}}"
  14. TOMCAT_BASE="${RUNTIME_DIR}/tomcat-base"
  15. TOMCAT_PID="${RUNTIME_DIR}/zq-tomcat.pid"
  16. MYSQL_LAUNCHD_LABEL="com.feifan.zq.full.mysql"
  17. TOMCAT_LAUNCHD_LABEL="com.feifan.zq.full.tomcat"
  18. log() {
  19. printf '[ZQ本机完整后台] %s\n' "$*"
  20. }
  21. is_running_pid() {
  22. local pid="${1:-}"
  23. [[ -n "${pid}" ]] && kill -0 "${pid}" 2>/dev/null
  24. }
  25. launchctl_domain() {
  26. printf 'gui/%s' "$(id -u)"
  27. }
  28. launchctl_available() {
  29. command -v launchctl >/dev/null 2>&1 && launchctl print "$(launchctl_domain)" >/dev/null 2>&1
  30. }
  31. launchctl_bootout_label() {
  32. local label="${1:-}"
  33. [[ -n "${label}" ]] || return 0
  34. launchctl_available || return 0
  35. launchctl bootout "$(launchctl_domain)/${label}" >/dev/null 2>&1 || true
  36. }
  37. find_redis_cli() {
  38. if [[ -n "${ZQ_REDIS_CLI:-}" && -x "${ZQ_REDIS_CLI}" ]]; then
  39. printf '%s' "${ZQ_REDIS_CLI}"
  40. elif command -v redis-cli >/dev/null 2>&1; then
  41. command -v redis-cli
  42. elif [[ -x "/opt/homebrew/bin/redis-cli" ]]; then
  43. printf '%s' "/opt/homebrew/bin/redis-cli"
  44. elif [[ -x "/usr/local/bin/redis-cli" ]]; then
  45. printf '%s' "/usr/local/bin/redis-cli"
  46. fi
  47. }
  48. redis_ping() {
  49. local cli
  50. cli="$(find_redis_cli || true)"
  51. [[ -n "${cli}" ]] || return 1
  52. "${cli}" -h 127.0.0.1 -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" --no-auth-warning ping 2>/dev/null | grep -q '^PONG$'
  53. }
  54. mysql_port_pids() {
  55. if command -v lsof >/dev/null 2>&1; then
  56. lsof -tiTCP:"${MYSQL_PORT}" -sTCP:LISTEN 2>/dev/null || true
  57. fi
  58. }
  59. mysql_data_lock_pids() {
  60. if command -v lsof >/dev/null 2>&1 && [[ -f "${RUNTIME_DIR}/mysql/data/ibdata1" ]]; then
  61. lsof -t "${RUNTIME_DIR}/mysql/data/ibdata1" 2>/dev/null || true
  62. fi
  63. }
  64. web_port_pids() {
  65. if command -v lsof >/dev/null 2>&1; then
  66. lsof -tiTCP:"${APP_PORT}" -sTCP:LISTEN 2>/dev/null || true
  67. fi
  68. }
  69. redis_port_pids() {
  70. if command -v lsof >/dev/null 2>&1; then
  71. lsof -tiTCP:"${REDIS_PORT}" -sTCP:LISTEN 2>/dev/null || true
  72. fi
  73. }
  74. launchctl_bootout_label "${TOMCAT_LAUNCHD_LABEL}"
  75. if [[ -f "${TOMCAT_PID}" ]] && is_running_pid "$(cat "${TOMCAT_PID}")"; then
  76. export CATALINA_HOME="${TOMCAT_HOME}"
  77. export CATALINA_BASE="${TOMCAT_BASE}"
  78. export CATALINA_PID="${TOMCAT_PID}"
  79. "${TOMCAT_HOME}/bin/catalina.sh" stop 20 -force >/dev/null 2>&1 || true
  80. log "已停止 ZQ Web 服务。"
  81. if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
  82. pids="$(web_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
  83. kill ${pids} >/dev/null 2>&1 || true
  84. sleep 2
  85. if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
  86. kill -9 ${pids} >/dev/null 2>&1 || true
  87. fi
  88. log "已清理 ZQ Web 残留进程。"
  89. fi
  90. else
  91. if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
  92. pids="$(web_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
  93. kill ${pids} >/dev/null 2>&1 || true
  94. sleep 2
  95. if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
  96. kill -9 ${pids} >/dev/null 2>&1 || true
  97. fi
  98. log "已清理 ZQ Web 残留进程。"
  99. else
  100. log "ZQ Web 服务当前没有运行。"
  101. fi
  102. rm -f "${TOMCAT_PID}"
  103. fi
  104. if redis_ping; then
  105. "$(find_redis_cli)" -h 127.0.0.1 -p "${REDIS_PORT}" -a "${REDIS_PASSWORD}" --no-auth-warning shutdown nosave >/dev/null 2>&1 || true
  106. log "已停止项目专用 Redis。"
  107. elif [[ -f "${REDIS_PID}" ]] && is_running_pid "$(cat "${REDIS_PID}")"; then
  108. kill "$(cat "${REDIS_PID}")" >/dev/null 2>&1 || true
  109. log "已停止项目专用 Redis。"
  110. elif [[ -n "$(redis_port_pids | tr -d '[:space:]')" ]]; then
  111. pids="$(redis_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
  112. kill ${pids} >/dev/null 2>&1 || true
  113. sleep 2
  114. if [[ -n "$(redis_port_pids | tr -d '[:space:]')" ]]; then
  115. kill -9 ${pids} >/dev/null 2>&1 || true
  116. fi
  117. log "已清理项目专用 Redis 残留进程。"
  118. rm -f "${REDIS_PID}"
  119. else
  120. log "项目专用 Redis 当前没有运行。"
  121. rm -f "${REDIS_PID}"
  122. fi
  123. launchctl_bootout_label "${MYSQL_LAUNCHD_LABEL}"
  124. if [[ -x "${MYSQL_HOME}/bin/mysqladmin" ]]; then
  125. if "${MYSQL_HOME}/bin/mysqladmin" --socket="${MYSQL_SOCKET}" -uroot ping >/dev/null 2>&1; then
  126. "${MYSQL_HOME}/bin/mysqladmin" --socket="${MYSQL_SOCKET}" -uroot shutdown >/dev/null 2>&1 || true
  127. log "已停止项目专用 MySQL。"
  128. elif [[ -n "$(mysql_port_pids | tr -d '[:space:]')" ]]; then
  129. pids="$(mysql_port_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
  130. kill ${pids} >/dev/null 2>&1 || true
  131. sleep 2
  132. if [[ -n "$(mysql_port_pids | tr -d '[:space:]')" ]]; then
  133. kill -9 ${pids} >/dev/null 2>&1 || true
  134. fi
  135. log "已清理项目专用 MySQL 残留进程。"
  136. rm -f "${RUNTIME_DIR}/mysql/mysql.pid" "${MYSQL_SOCKET}" "${MYSQL_SOCKET}.lock"
  137. else
  138. log "项目专用 MySQL 当前没有运行。"
  139. rm -f "${RUNTIME_DIR}/mysql/mysql.pid" "${MYSQL_SOCKET}" "${MYSQL_SOCKET}.lock"
  140. fi
  141. if [[ -n "$(mysql_data_lock_pids | tr -d '[:space:]')" ]]; then
  142. pids="$(mysql_data_lock_pids | tr '\n' ' ' | sed 's/[[:space:]]*$//')"
  143. kill ${pids} >/dev/null 2>&1 || true
  144. sleep 2
  145. if [[ -n "$(mysql_data_lock_pids | tr -d '[:space:]')" ]]; then
  146. kill -9 ${pids} >/dev/null 2>&1 || true
  147. fi
  148. log "已清理项目专用 MySQL 数据文件残留占用。"
  149. fi
  150. else
  151. log "没有找到 MySQL 管理命令。"
  152. fi
  153. remaining=""
  154. if [[ -n "$(web_port_pids | tr -d '[:space:]')" ]]; then
  155. remaining="${remaining} ZQ-Web(${APP_PORT})"
  156. fi
  157. if [[ -n "$(redis_port_pids | tr -d '[:space:]')" ]]; then
  158. remaining="${remaining} Redis(${REDIS_PORT})"
  159. fi
  160. if [[ -n "$(mysql_port_pids | tr -d '[:space:]')" || -n "$(mysql_data_lock_pids | tr -d '[:space:]')" ]]; then
  161. remaining="${remaining} MySQL(${MYSQL_PORT})"
  162. fi
  163. if [[ -n "${remaining}" ]]; then
  164. log "仍有残留进程没有退出:${remaining}。请关闭正在打开的“ZQ本机完整后台”终端窗口,或重启电脑后再双击启动。"
  165. exit 1
  166. fi