less than 1 minute read

Hello,

While developing on Linux I often find myself searching for the process id of the process that keeps a port occupied. I always forget the commands and I have to Google them or ask an LLM.

So I’ve asked a LLM to come with a bash to automate the process, and it came up with:

killport() {
if [ -z "$1" ]; then
echo "Usage: killport <port_number>"
echo "Example: killport 4000"
return 1
fi

    local port=$1 
 
    # Get PIDs listening on the port 
    local pids=$(lsof -ti:"$port" 2>/dev/null) 
 
    if [ -z "$pids" ]; then 
        echo "No process found listening on port $port" 
        return 0 
    fi 
 
    echo "Killing processes on port $port (PIDs: $pids)" 
    kill -9 $pids 
    echo "Done." 
}

Then I added the function to my zshrc file.

Now whenever I need to shut down the application keeping a certain port busy I issue the killport command:

killport 8080

Thanks for reading!