Skip to content

Instantly share code, notes, and snippets.

@spadin
Last active December 7, 2018 17:18
Show Gist options
  • Save spadin/cb3efe5b724b9e1846fbec11e8b1cb24 to your computer and use it in GitHub Desktop.
Save spadin/cb3efe5b724b9e1846fbec11e8b1cb24 to your computer and use it in GitHub Desktop.
End the process that's using a specific port.
#! /usr/bin/env bash
function kill-port () {
local length=$(($#-1))
local killargs=( "${@:1:$length}" )
shift $(($# - 1))
local port=$1
local pid
pid=$(lsof -t -i :"$port")
if [ "$pid" = "" ]; then
echo "Port $port isn't being used."
exit 0;
fi
kill "${killargs[@]}" "$pid"
echo "PID $pid, matching port $port was terminated."
}
kill-port "$@"
@spadin
Copy link
Author

spadin commented Dec 7, 2018

Nice. I didn't know about shellcheck. Found kill-port -s KILL 1234 wasn't working, but it is now. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment