DEV Community

Cover image for kill any port from terminal šŸ„·
Orhun Ɩzer
Orhun Ɩzer

Posted on

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)