Find the Process Listening to Port on Mac OS X

How to Find the Process Listening to Port on Mac OS X

The lsof command can be used to display the process PID

Step-by-Step

To find the process that is listening to a port on Mac OS X, we’ll use the lsof command to find the process ID (PID), and the ps command to show the name.

Find the Process ID (PID)

 There are two different ways we can use to find the process that is listening to a port on Mac OS X.

Find the Process ID (PID) Using lsof

Using the lsof command we can find the process ID (PID) for a specific port that is in a LISTEN state. In a terminal type the following and replace the “<port-number>” with our port number.

lsof -nP -iTCP -sTCP:LISTEN | grep <port-number>

This generates output that looks like this:

node 63851 pogo 27u IPv6 0xfded4774db1c601f 0t0 TCP *:9999 (LISTEN)

In the output above the PID (process ID) is the second value, in this example output the process ID (PID) is “63851”. This command will also print out the port number, which is 9999 in the above output example.

Find the Process ID (PID) Using netstat

Using the nestat command we can find the process ID (PID) for a specific port. In a terminal type the following and replace the “<port-number>” with our port number.

netstat -anv | grep <port-number>

This generates output that looks like this:

tcp46 0 0 *.9999 *.* LISTEN 131072 131072 63851 0 0x0100 0x00000106

In the output above the PID (process ID) is the ninth value (the fourth value from the end), in this example output the process ID (PID) is “63851”. This command will also print out the port number, which is 9999 in the above output example.

Find the Process Name

We can now use the process status command ps to display the process name for the process ID (PID).

ps -Ao user,pid,command | grep -v grep | grep <PID>

This generates output that looks like this:

mymachine 63851 the-process

In the output above the process name is the last value “the-process.” Now we know the name of the process that is listening to the port. The reason as to why the grep command is listed twice is to avoid displaying the process ID (PID) for the grep command itself.

Other Useful Commands

How to Kill or Stop the Process by PID

You can kill the process by process ID (PID) using the kill command. Replace “<PID>” with the process ID from lsof or netstat.

kill -9 <PID>

The lsof Command

The lsof command lists open files. Network sockets count as files, so each open network socket, either listening or actively in use is listed by lsof. In addtion you can run the man lsof command to display all the different options for lsof.

man lsof

lsof can take a very long time to execute, so I suggest that you use -n (inhibits the conversion of network numbers to host names for network files) and -P (inhibits the conversion of port numbers to port names for network files) to speed it up. 

lsof -nP

Don’t be a Tool

Developers often associate their identity with a programming language or a framework. I’m a Java developer, I’m a JavaScript developer, I’m a React developer, or I’m a back-end developer. It’s an understandable perspective, however, if you asked a carpenter what he does for a living, and he answered, “I’m a user of hammers,” it wouldn’t …

Continue reading

Unexpected Token ‘<' client_bundle.js:1

Unexpected Token ‘<‘, I got this message while I was working on one of my React / Webpack projects on Churgle, a website that has tools and utilities. The error in my case manifested itself on single a page reachable through a React route. It didn’t matter if I changed the order of any routes, …

Continue reading

External File Sort In Java

How do you sort a lot of data in Java? It’s easy to sort small amounts of data in memory. For example if your data is in a List object, then with the help of the Collections class you can easily sort the data. You can provide a custom Comparator to control in what order your data …

Continue reading

Los Angeles Hadoop Users Group- LA-HUG

LA now has its own HUG. The first meetup will be held on Wednesday 2/9/2011. This is a great opportunity for anyone in the Los Angeles area with interest in Hadoop and related technologies to discuss and meet. The first talk is:  “Operationalizing Hadoop” with Charles Zedlewski (Cloudera’s VP Product) http://www.meetup.com/LA-HUG/

Continue reading

fatal: The remote end hung up unexpectedly

I committed changes to my GIT project, tried to push them to the remote server (git push) and got the following cryptic error message: fatal: The remote end hung up unexpectedly The GitFaq states that: Git push fails with “fatal: The remote end hung up unexpectedly”? If, when attempting git push, you get a message …

Continue reading

Hadoop World

I just came back from the Hadoop World conference in New York and I have to say that it was quite exciting. Processing huge amounts of data used to be a problem for just a few companies like Google, Yahoo, Facebook and a few others, but has now become a problem for many. The conference …

Continue reading

ls full path

How do you get the Unix command ls to show you the full path? Unfortunately there’s no argument for ls that will do this directly. However this will work fine and give you what you want. ls -d $PWD/* or ls -ld $PWD/*

Continue reading

Window Stuck Under Toolbar in OS X

Sometimes a window can get stuck under the top toolbar in OS X. This often happens when I use Citrix in OS X to run Windows applications. When this happens it’s not possible to grab the window nor  to close it. A simple solution for this is to press [fn] [shift] [F2] which will move …

Continue reading