Skip to content

Instantly share code, notes, and snippets.

@zwhitchcox
Created June 11, 2018 13:06
Show Gist options
  • Save zwhitchcox/dc05e89567c0bdb1e50a59c8c6873796 to your computer and use it in GitHub Desktop.
Save zwhitchcox/dc05e89567c0bdb1e50a59c8c6873796 to your computer and use it in GitHub Desktop.
Force kill process by port in one command
#!/bin/bash
# force kill process by port
# Usage: "force-kill PORT"
sudo netstat -tulnp | grep "$1" | sed -r -e "s/.*LISTEN\\s+([0-9]+).*/\1/" | xargs sudo kill -9
@bartlett-ops
Copy link

Your version could match multiple ports. Using the input 80 would kill the processes for 80 and 8080.
sudo netstat -tulnp | grep -E ":$1 .*" | grep -Eo '[^/]+$' | xargs sudo kill -9

@Winona-Ryder
Copy link

@zwhitchcox, I think this is the same thing as fuser $PORT/tcp --kill except without the usual shellscript bugs or need for sudo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment