Skip to content

Instantly share code, notes, and snippets.

@spadin

spadin/kill-port

Last active Dec 7, 2018
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@soulcutter 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

This comment has been minimized.

Copy link
Owner Author

@spadin 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