Skip to content

Instantly share code, notes, and snippets.

@jschaf
Last active November 22, 2021 23:50
Show Gist options
  • Save jschaf/b690361149dd6427d26f97005ff50e1a to your computer and use it in GitHub Desktop.
Save jschaf/b690361149dd6427d26f97005ff50e1a to your computer and use it in GitHub Desktop.
port-kill
#!/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