sajad torkamani
sudo lsof -i tcp:8080 | awk 'NR!=1 {print $2}' | xargs -r kill 
  • lsof -i tcp:8080 – list all processes that is listening on TCP port 8080
  • awk 'NR!=1 {print $2}' – ignore first line and print second column of each line
  • xargs -r kill – pipe the port numbers as an argument to kill.

Bonus: add shell alias

function killport() {
  lsof -i tcp:$1 | awk 'NR!=1 {print $2}' | xargs -r kill 
}

Now, you can do killport 8080.

Sources

Tagged: Unix

Leave a comment

Your email address will not be published. Required fields are marked *