-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
54 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters