Skip to content

Instantly share code, notes, and snippets.

@indianajone
Created May 8, 2019 04:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save indianajone/1c1e25051858df89aa47ccda5ca81b96 to your computer and use it in GitHub Desktop.
Save indianajone/1c1e25051858df89aa47ccda5ca81b96 to your computer and use it in GitHub Desktop.
Find (and kill) all processes listening on a port

To search for processes that listen on a specific port use the lsof or “List Open Files”. The -n argument makes the command run faster by preventing it from doing a ip to hostname conversion. Use grep to show only lines containing the word LISTEN.

lsof -n | grep LISTEN

To filter for a specific port use the following:

lsof -n -i4TCP:[PORT] | grep LISTEN

To kill all processes listening on a specific port use:

lsof -n -i4TCP:[PORT] | grep LISTEN | awk '{ print $2 }' | xargs kill

The awk command returns only the second column (PID), and the xargs executes kill on each line returned.

@ref: https://til.hashrocket.com/posts/e4c8c665a8-find-and-kill-all-processes-listening-on-a-port

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