Last Updated: February 25, 2016
·
969
· robsonmwoc

Find a process that is listening to a port

If sometimes, you got depressed because the rails server command insists in tell you, that a certain port is already in use. But you're sure about to close all ruby process?

I've got a tip for you.

Probably, the process was detached from the console, but it's keep running in background, or there is some initialized process that you don't know its existence, using such port.

I usually try to figure out this kind of problem using lsof, that's a native command for MacOSX and Linux, that tells you which files are attached to a specific port. To know more about it refer to the man page http://unixhelp.ed.ac.uk/CGI/man-cgi?lsof+8

So, you can do something like this:

$ lsof -i :80
COMMAND PID USER FD TYPE  DEVICE SIZE/OFF NODE NAME
apache2 511 www-data 3u IPv4 6470920 0t0 TCP *:www (LISTEN)
apache2 524 www-data 3u IPv4 6470920 0t0 TCP *:www (LISTEN)

That was my first tip, and I hope get a lot more to share.

Cheers.

2 Responses
Add your response

helped alot thanks :)

over 1 year ago ·

$ netstat -tnlp is helpful too, which shows all programs (-p) listening (-l) on TCP (-t) ports in numeric format (-n). However I've found that 'netstat' can be unreliable on occasions, e.g., false negatives.

This is better way if you know the port number you want to check for. Alternatively you can use: $ lsof -i :1-65535 to check the full port range instead of just one. Or: lsof -i TCP:1-65535 to check only TCP ports.

over 1 year ago ·