Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estevanjantsk/095ee2c99df00dfcd16a0cc4c147edcb to your computer and use it in GitHub Desktop.
Save estevanjantsk/095ee2c99df00dfcd16a0cc4c147edcb 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