diff --git a/.evergreen/csfle/stop-servers.sh b/.evergreen/csfle/stop-servers.sh index 7f92bfec..4ff176e8 100755 --- a/.evergreen/csfle/stop-servers.sh +++ b/.evergreen/csfle/stop-servers.sh @@ -12,3 +12,10 @@ if [ -f "kmip_pids.pid" ]; then rm kmip_pids.pid fi popd + +# Forcibly kill the process listening on the desired ports, most likely +# left running from a previous task. +. "$SCRIPT_DIR/../process-utils.sh" +for port in 5698 9000 9001 9002 8080; do + killport $port 9 +done diff --git a/.evergreen/process-utils.sh b/.evergreen/process-utils.sh new file mode 100755 index 00000000..800270a1 --- /dev/null +++ b/.evergreen/process-utils.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# +# process-utils.sh +# +# Usage: +# . /path/to/process]-utils.sh +# +# This file defines the following functions: +# - killport +# These functions may be invoked from any working directory. + + +# killport +# +# Usage: +# killport 8889 +# +# Parameters: +# "$1": The port of the process to kill. +# "$2": The signal to send, defaults to SIGTERM. +# +# Kill the process listening on the given port. +killport() { + local -r port="${1:?'killport requires a port'}" + local -r signal="${2:-15}" + local pid="" + + if [[ "${OSTYPE:?}" == cygwin || "${OSTYPE:?}" == msys ]]; then + for pid in $(netstat -ano | grep ":$port .* LISTENING" | awk '{print $5}' | tr -d '[:space:]'); do + taskkill /F /T /PID "$pid" || true + done + elif [ -x "$(command -v lsof)" ]; then + for pid in $(lsof -t "-i:$port" || true); do + kill "$pid" -${signal} || true + done + elif [ -x "$(command -v fuser)" ]; then + fuser --kill -${signal} "$port/tcp" || true + elif [ -x "$(command -v ss)" ]; then + for pid in $(ss -tlnp "sport = :$port" | awk 'NR>1 {split($7,a,","); print a[1]}' | tr -d '[:space:]'); do + kill "$pid" -${signal} || true + done + else + echo "Unable to identify the OS (${OSTYPE:?}) or find necessary utilities (fuser/lsof/ss) to kill the process." + exit 1 + fi +} diff --git a/.evergreen/start-orchestration.sh b/.evergreen/start-orchestration.sh index 315f027a..f774f015 100755 --- a/.evergreen/start-orchestration.sh +++ b/.evergreen/start-orchestration.sh @@ -51,45 +51,12 @@ if [[ "${OSTYPE:?}" == cygwin ]]; then ORCHESTRATION_ARGUMENTS="$ORCHESTRATION_ARGUMENTS -s wsgiref" fi - -# killport -# -# Usage: -# killport 8889 -# -# Parameters: -# "$1": The port of the process to kill. -# -# Kill the process listening on the given port. -killport() { - local -r port="${1:?'killport requires a port'}" - local pid="" - - if [[ "${OSTYPE:?}" == cygwin || "${OSTYPE:?}" == msys ]]; then - for pid in $(netstat -ano | grep ":$port .* LISTENING" | awk '{print $5}' | tr -d '[:space:]'); do - taskkill /F /T /PID "$pid" || true - done - elif [ -x "$(command -v lsof)" ]; then - for pid in $(lsof -t "-i:$port" || true); do - kill "$pid" || true - done - elif [ -x "$(command -v fuser)" ]; then - fuser --kill -SIGTERM "$port/tcp" || true - elif [ -x "$(command -v ss)" ]; then - for pid in $(ss -tlnp "sport = :$port" | awk 'NR>1 {split($7,a,","); print a[1]}' | tr -d '[:space:]'); do - kill "$pid" || true - done - else - echo "Unable to identify the OS (${OSTYPE:?}) or find necessary utilities (fuser/lsof/ss) to kill the process." - exit 1 - fi -} - # Forcibly kill the process listening on port 8889, most likely a wild # mongo-orchestration left running from a previous task. # NOTE: Killing mongo-orchestration with SIGTERM gives it a chance to # cleanup any running mongo servers. # Also kill any leftover mongo processes on common ports as a backup. +. "$det_evergreen_dir/process-utils.sh" for port in 8889 27017 27018 27019 27217 27218 27219 1026; do killport $port done