stop_translator.sh 1.08 KB
#!/bin/bash
#
# Stop Translation Service
#

set -e

cd "$(dirname "$0")/.."

PID_FILE="logs/translator.pid"
TRANSLATION_PORT="${TRANSLATION_PORT:-${TRANSLATOR_PORT:-6006}}"

echo "========================================"
echo "Stopping Translation Service"
echo "========================================"

if [ -f "${PID_FILE}" ]; then
  PID="$(cat "${PID_FILE}" 2>/dev/null || true)"
  if [ -n "${PID}" ] && kill -0 "${PID}" 2>/dev/null; then
    echo "Stopping PID from file: ${PID}"
    kill -TERM "${PID}" 2>/dev/null || true
    sleep 1
    if kill -0 "${PID}" 2>/dev/null; then
      kill -KILL "${PID}" 2>/dev/null || true
    fi
  fi
  rm -f "${PID_FILE}"
fi

PORT_PIDS="$(lsof -ti:${TRANSLATION_PORT} 2>/dev/null || true)"
if [ -n "${PORT_PIDS}" ]; then
  echo "Stopping process on port ${TRANSLATION_PORT}: ${PORT_PIDS}"
  for PID in ${PORT_PIDS}; do
    kill -TERM "${PID}" 2>/dev/null || true
  done
  sleep 1
  PORT_PIDS="$(lsof -ti:${TRANSLATION_PORT} 2>/dev/null || true)"
  for PID in ${PORT_PIDS}; do
    kill -KILL "${PID}" 2>/dev/null || true
  done
fi

echo "Translation service stopped."