July 22, 2024

/ ~

Kill the running application using the port number

When I close my applications running in the terminal, sometimes ports don’t die and killall node doesn’t work.

You can use kill to force close the port.

Terminal window
sudo kill $(sudo lsof -t -i:{PORT})

You can then assign aliases to simplify usage.

Terminal window
alias killport="killport_function"
killport_function ()
{
sudo kill $(sudo lsof -t -i:$1)
}

Put the above code in your ~/.zshrc file and run source ~/.zshrc. You can now use it as follows.

Terminal window
killport 3000