44 lines
1.9 KiB
Bash
Executable File
44 lines
1.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
cd -- "$(dirname -- "${BASH_SOURCE[0]}")"
|
|
PORT=7777
|
|
|
|
echo
|
|
echo "================================================================================"
|
|
echo " Commonwealth Online - Port ${PORT} in Use"
|
|
echo "================================================================================"
|
|
echo "1. Kill the process using TCP ${PORT} and restart"
|
|
echo "2. Start the server on a different port"
|
|
echo "3. Cancel"
|
|
read -r -p "Select option (1-3): " choice
|
|
|
|
find_pids_on_port() {
|
|
local port="$1" pids=""
|
|
if command -v lsof >/dev/null 2>&1; then pids=$(lsof -tiTCP:"${port}" -sTCP:LISTEN 2>/dev/null || true); fi
|
|
if [[ -z "${pids}" ]] && command -v fuser >/dev/null 2>&1; then pids=$(fuser "${port}/tcp" 2>/dev/null | tr -s '[:space:]' '\n' | grep -E '^[0-9]+$' || true); fi
|
|
if [[ -z "${pids}" ]] && command -v ss >/dev/null 2>&1; then pids=$(ss -lptn "sport = :${port}" 2>/dev/null | sed -n 's/.*pid=\([0-9]\+\).*/\1/p' | sort -u || true); fi
|
|
printf '%s\n' "${pids}"
|
|
}
|
|
|
|
case "${choice}" in
|
|
1)
|
|
pids=$(find_pids_on_port "${PORT}")
|
|
[[ -n "${pids}" ]] || { echo "Could not determine the process using TCP ${PORT}." >&2; exit 1; }
|
|
echo "Stopping process(es): ${pids}"
|
|
# shellcheck disable=SC2086
|
|
kill -9 ${pids} 2>/dev/null || { echo "Could not kill the process. Try with sufficient permissions." >&2; exit 1; }
|
|
sleep 2
|
|
exec bash ./start.sh
|
|
;;
|
|
2)
|
|
read -r -p "Enter desired port (1024-65535): " new_port
|
|
[[ "${new_port}" =~ ^[0-9]+$ ]] || { echo "Port must be numeric." >&2; exit 1; }
|
|
(( new_port >= 1024 && new_port <= 65535 )) || { echo "Port must be 1024-65535." >&2; exit 1; }
|
|
echo "Starting on TCP/UDP ${new_port}. This command-line override does not rewrite commonwealth-server.json."
|
|
exec bash ./start.sh --port "${new_port}"
|
|
;;
|
|
3) exit 0 ;;
|
|
*) echo "Invalid option." >&2; exit 1 ;;
|
|
esac
|