Thread: Find process using port
03-30-2009, 11:04 PM #1
Find process using port
- Member Since
- Dec 31, 2008
What are the ways to find which process is using which network port? Is there a known issue where the PID can't be found?
I have a custom written process (written in Java although shouldn't matter) which many times after exiting (but not always) its listen port is still shown as LISTEN in a netstat -an output. The process is gone though (I have exited it).
Here is a little snippet of a command-line session after the process has quit (my process listens on port 8181):
You can see that something is still listening on port 8181 because I can telnet to it.
mjparme:~:4> telnet localhost 8181
Connected to localhost.
Escape character is '^]'.
However a lsof doesn't find it (a sudo lsof | grep 8181 doesn't return results either)
mjparme:~:5> sudo lsof -i TCP:8181
Nothing in ps shows that port (although the port is read from a property file rather than the command line so wouldn't really expect to see it here)
mjparme:~:6> ps auxww | grep 8181
mjparme 608 0.0 0.0 599780 392 s000 R+ 5:34PM 0:00.00 grep 8181
The netstat still shows it listening. If netstat shows it why doesn't lsof show it?
mjparme:~:7> netstat -an | grep 8181
tcp6 5 0 ::1.8181 ::1.54606 CLOSE_WAIT
tcp6 0 0 ::1.8181 ::1.54605 CLOSE_WAIT
tcp4 0 0 127.0.0.1.8181 127.0.0.1.51365 CLOSE_WAIT
tcp46 0 0 *.8181 *.* LISTEN
tcp6 0 0 ::1.54606 ::1.8181 FIN_WAIT_2
tcp6 0 0 ::1.54605 ::1.8181 FIN_WAIT_2
Some flavors of the netstat command accepts a -p option which shows the PID of the owning process, the Mac OS version doesn't appear to do that.
My main question: Is there another way to find the PID of the process that is still LISTENing on that port?
This process runs great on Windows and Linux (actually runs on Linux in production). I recently got a Mac at work so have started to do development on my Mac. (On windows on linux when the process exits that port is no longer LISTENing)
I am unsure if I am dealing with a Mac OS bug or an Apple JVM bug. Or maybe I have a coding error that is exposed in the apple JVM but not on windows or linux.
10-15-2009, 05:34 PM #2
Try lsof ....
- Member Since
- Oct 15, 2009
I just had exactly the same problem as you and found a solution (at Linux / UNIX Find Out What Program / Service is Listening on a Specific TCP Port)
You can short network connections with lsof on OS X as well:
lsof -Pnl +M -i4
Grep for your port of interest and you are done.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
By Mac9346 in forum OS X - Operating SystemReplies: 10Last Post: 08-14-2013, 12:35 PM
By CatalinaAlonso in forum OS X - Operating SystemReplies: 3Last Post: 04-19-2009, 03:35 PM
By joe.morgan in forum OS X - Operating SystemReplies: 3Last Post: 01-30-2007, 08:25 PM
By Rhetro in forum Schweb's LoungeReplies: 0Last Post: 09-19-2006, 05:00 PM
By walkerj in forum Switcher HangoutReplies: 0Last Post: 04-23-2005, 05:22 PM