Last active
November 22, 2021 23:50
-
-
Save jschaf/b690361149dd6427d26f97005ff50e1a to your computer and use it in GitHub Desktop.
port-kill
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
#!/bin/zsh | |
function port-kill() { | |
local port="$1" | |
local pids | |
# Simplest way to fill an array from a command. The results are whitespace | |
# safe. | |
# shellcheck disable=SC2207 | |
pids=( $(lsof -t -i:"${port}") ) | |
if [[ "${#pids[@]}" == 0 ]]; then | |
echo 'No pids found on port' ${port} | |
return 1 | |
fi | |
print -- "Killing PIDs ${pids[*]} running on port ${port}..." | |
for pid in "${pids[@]}"; do | |
kill "${pid}" | |
done | |
printf ' done\n' | |
} | |
port-kill "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment