DEV Community

Olaniyi Philip Ojeyinka
Olaniyi Philip Ojeyinka

Posted on

create an alias to force Kill a port

add to your bash_profile, or basrc file etc.


port_kill() {
    if [ -z "$1" ]; then
        echo "Usage: port_kill PORT_NUMBER"
        return 1
    fi

    PIDs=$(lsof -t -i :"$1" 2>/dev/null)
    if [ -n "$PIDs" ]; then
        kill -9 $PIDs 2>/dev/null
        echo "✓ Successfully killed process(es) running on port $1"
        return 0
    else
        echo "No process running on port $1"
        return 1
    fi
}
alias force-kill='port_kill'

Enter fullscreen mode Exit fullscreen mode

Top comments (0)