Skip to content

Instantly share code, notes, and snippets.

@Alfreddd
Created June 20, 2012 17:31
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Alfreddd/2961071 to your computer and use it in GitHub Desktop.
Save Alfreddd/2961071 to your computer and use it in GitHub Desktop.
list/count open connections (sockets) for All/certain process
# list open sockets for PID
sudo lsof -p 13264 | egrep 'TCP|UDP'
# count of sockets
sudo lsof -p 13264 | egrep "TCP|UDP" | wc -l
# list the open sockets for all passengers
passenger-status | grep PID | awk '{ print $3}' | xargs -i sudo lsof -p {} | egrep 'TCP|UDP'
# Alternative command
sudo netstat -anp --inet
@Decstasy
Copy link

You can use grep with -c argument to count your results. You can spare another process call.
sudo lsof -p 13264 | egrep -c "TCP|UDP" is the same as sudo lsof -p 13264 | egrep "TCP|UDP" | wc -l

@insinfo
Copy link

insinfo commented Aug 5, 2022

I'm trying to formulate a command line to give me the number of open sockets/files per process, that is, give me a list with a column with the process name and another column with the number of open sockets, can you help me with that.

ps ax | awk '{ print $1}' | xargs -i sudo lsof -p {} | egrep 'TCP|UDP'

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