Skip to content

Commit

Permalink
DRIVERS-2991 Prefer lsof over fuser for macos support (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneHarvey authored Sep 27, 2024
1 parent acd65f9 commit cafef5e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
65 changes: 44 additions & 21 deletions .evergreen/start-orchestration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,52 @@ 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
pid=$(netstat -ano | grep ":$port .* LISTENING" | awk '{print $5}' | tr -d '[:space:]')
if [ -n "$pid" ]; then
taskkill /F /T /PID "$pid" || true
fi
elif [ -x "$(command -v lsof)" ]; then
pid=$(lsof -t "-i:$port" || true)
if [ -n "$pid" ]; then
kill "$pid" || true
fi
elif [ -x "$(command -v fuser)" ]; then
fuser --kill -SIGTERM "$port/tcp" || true
elif [ -x "$(command -v ss)" ]; then
pid=$(ss -tlnp "sport = :$port" | awk 'NR>1 {split($7,a,","); print a[1]}' | tr -d '[:space:]')
if [ -n "$pid" ]; then
kill "$pid" || true
fi
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.
if [[ "${OSTYPE:?}" == cygwin || "${OSTYPE:?}" == msys ]]; then
OLD_MO_PID=$(netstat -ano | grep ':8889 .* LISTENING' | awk '{print $5}' | tr -d '[:space:]')
if [ ! -z "$OLD_MO_PID" ]; then
taskkill /F /T /PID "$OLD_MO_PID" || true
fi
elif [ -x "$(command -v fuser)" ]; then
fuser --kill 8889/tcp || true
elif [ -x "$(command -v lsof)" ]; then
OLD_MO_PID=$(lsof -t -i:8889 || true)
if [ ! -z "$OLD_MO_PID" ]; then
kill -9 "$OLD_MO_PID" || true
fi
elif [ -x "$(command -v ss)" ]; then
OLD_MO_PID=$(ss -tlnp 'sport = :8889' | awk 'NR>1 {split($7,a,","); print a[1]}' | tr -d '[:space:]')
if [ ! -z "$OLD_MO_PID" ]; then
kill -9 "$OLD_MO_PID" || true
fi
else
echo "Unable to identify the OS (${OSTYPE:?}) or find necessary utilities (fuser/lsof/ss) to kill the process."
exit 1
fi
# 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.
for port in 8889 27017 27018 27019 27217 27218 27219 1026; do
killport $port
done


mongo-orchestration $ORCHESTRATION_ARGUMENTS start > $MONGO_ORCHESTRATION_HOME/out.log 2>&1 < /dev/null &

Expand Down
4 changes: 3 additions & 1 deletion .evergreen/stop-orchestration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ cd ${DRIVERS_TOOLS}

# source the mongo-orchestration virtualenv if it exists
VENV="$MONGO_ORCHESTRATION_HOME/venv"
if [ -f "$VENV/bin/activate" ]; then
if [ -x "$(command -v mongo-orchestration)" ]; then
mongo-orchestration stop
elif [ -f "$VENV/bin/activate" ]; then
. "$VENV/bin/activate"
mongo-orchestration stop
elif [ -f "$VENV/Scripts/activate" ]; then
Expand Down

0 comments on commit cafef5e

Please sign in to comment.