How to find out which port number is being used by a process
I want to find out which port number the process is and filter the results using a keyword.
For example, I can quickly find out which port numbers are being used by "node" js applications.
It didn't work:
netstat tulnap | grep "node"
This did not return port numbers:
ps aux | grep node
source to share
This is how I found the solution:
ยป lsof -i -P | grep node
node 14489 me 12u IPv4 0x... 0t0 TCP *:4000 (LISTEN)
Also, if I knew the port and I was looking for the process name, I would:
ยป lsof -i :4000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 14489 me 12u IPv4 0x... 0t0 TCP *:terabase (LISTEN)
source to share
What you are doing will also never work as GREP is never guaranteed to provide one, one, or more than 1 result.
But in the context of your particular case, any process is truly unique and has a unique number. So, first you have to do a search to find the process number.
Doing a simple grep will still give you none, none, or multiple process numbers. But you need to find an additional filter so that you get 1 process number.
If you have 1 process number, you can check the port (s) used by that process.
What makes you think "node" is reported in PS? It also cannot be.
source to share