DEV Community

Cover image for kill any port from terminal 🥷
Orhun Özer
Orhun Özer

Posted on

1 1

kill any port from terminal 🥷

killport() {
    if [[ $# -eq 0 ]]; then
        echo "Usage: killport <port>"
        return 1
    fi

    local port=$1
    local pid=$(lsof -n -i4TCP:"$port" | awk 'NR>1 {print $2}')

    if [[ -n $pid ]]; then
        kill -9 "$pid"
        echo "Process with PID $pid killed."
    else
        echo "No process found running on port $port."
    fi
}
Enter fullscreen mode Exit fullscreen mode

Add this to .zshrc

Call killport 8080 from terminal.

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay