Skip to content

Instantly share code, notes, and snippets.

@spadin
Last active December 7, 2018 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 "$@"
@soulcutter
Copy link

soulcutter commented Dec 6, 2018

$ shellcheck ./kill-port.sh

In ./kill-port.sh line 5:
    local killargs=${@:1:$length}
                   ^-- SC2124: Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.


In ./kill-port.sh line 9:
    local pid=$(lsof -t -i :$port)
          ^-- SC2155: Declare and assign separately to avoid masking return values.
                            ^-- SC2086: Double quote to prevent globbing and word splitting.


In ./kill-port.sh line 16:
    kill $killargs $pid
         ^-- SC2086: Double quote to prevent globbing and word splitting.
                   ^-- SC2086: Double quote to prevent globbing and word splitting.


In ./kill-port.sh line 20:
kill-port $@
          ^-- SC2068: Double quote array expansions to avoid re-splitting elements.

@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