Finding a process using a specific network port with the lsof command

Today I could not launch a node server: the port my app was targeted was already in use. Fortunately Unix comes with very handy command line tools when it comes to finding which processes are using a specific port/address.

No matter what I did, refused to start with the error:

The only way to fix the problem was to either use another port for my app (I didn’t want to do that) or find the process which used this port and kill it. I’m quite familiar with Unix command line but didn’t know how to do that. After the research, I found the lsof command:

At first it didn’t seem to be useful, but looking at the command line arguments, there is one that’s interesting:

Sounds like it will be useful after all!

Let’s try:

Since at any time there are a lot of connections going on, this returns quite a lot of entries. But we may easily filter them, by using grep. Since the process is waiting for connections, it is likely in LISTEN mode, so we can type:

That’s better: we have two different node processes listening. The good thing about the -i option is that we may add the port as well:

We got it! These two processes are using port 8080, for some reason, pm2 didn’t correctly kill them. But we can easily do so ourselves with:

Note that killing the first one will automatically kill the second process.


Warpdesign.fr