kill any port from terminal 🥷

orabazu

Orhun Özer

Posted on May 17, 2023

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.

💖 💪 🙅 🚩
orabazu
Orhun Özer

Posted on May 17, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related