Find out all running node.js application with pid and port - node.js

I have to find all running node.js app on machine with pid and port along with path and then following path I would like to find all classes then functions inside classes. Example suppose we have 2 separate independent node application with different folder and want to find as mentioned above.I tried hard but not find solution. I have to implement this functionality for client project. Is this possible and if possible then how it be done?
Any insight would be appreciated.
Thanks! Any help is appreciated

Yes, there are a couple of ways to do this -
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
pgrep node
Process command (ps)
ps ax will give the full list of processes and adding a 'u' option gives detailed information. i.e. ps aux
To search for a particular process grep command is used, so for nodejs, it would be ps aux | grep node
Network Statistics (netstat)
netstat -a | more : To show both listening and non-listening sockets
netstat -at : To list all tcp ports.
netstat -l : To list only the listening ports.
netstat -lt : To list only the listening tcp ports.
netstat -pt : To display the PID and program names
So as chris-lam has suggested netstat -lntp | grep node would list all the listening TCP ports running as a node process.
To use it within code following piece of code can be helpful -
const {exec} = require('child_process');
exec('ps aux | grep node', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});

You can find the port and PID with the netstat command.
netstat -lntp | grep node
ps allows you to find the full command of the process:
ps aux | grep node

After some googling and researching, this is the way I found the easier one for me.
I didn't found yet a one-liner, so my solution is composed by 2 commands:
ps aux | grep node
it wil return all node process with pids and paths
netstat -lntp | grep node
it will return pids and ports
Using them together with
ps aux | grep node && netstat -lntp | grep node
Will give you something like this:
So if you want to know which app is using which port, you can look for the port from the output of second command, take the pid from the last column and from that you can lookup the pid in the output from first command: from there you should be able to understand the path of the program using that port.
It's not very elegant but it's the easier way I found

Related

Display PID and Port of running processes

I have a task where I have to display the PID and Port number of running processes/tasks in Linux. What I have done so far is:
ps -ef | awk '/[t]ick/{print "PID="$2" "$9}'
Can anyone tell me how do I also display the port of the returned results? Here is what Im getting returned currently:

listen EADDRINUSE: address already in use :::5000

In my application, I use concurrently to run both backend and front end simultaneously. After ctrl + c, strill the port 5000 is running. Also, port 3000 is running. I have to manually kill processes. How can I solve this?
Run cmd.exe as 'Administrator':
C:\Windows\System32>taskkill /F /IM node.exe
run pa -xa | grep node
you will get result with processid
4476 pts/0 Sl+ 0:01 node index.js
then kill the process with kill -9 4476
as simple as that
lsof -ti finds open files(sockets are files in nix based systems) -t removes the headers, so that we can pipe into kill(We just want the process id), -i lets lsof find the file based off the internet address. We do not have to provide the full address, we can just search by port, by using the pattern :port.
Some commands accept input from stdin, and we can pipe directly to them, kill is not one of those commands, so we must use xargs(It reads from stdin, and calls the specified command with the input from stdin).
Finally the ; lets us execute both commands irrespective of one another. Regardless of whether lsof -ti:3000 | xargs kill succeeds or fails,
lsof -ti:5000 | xargs kill will run, and vice versa.
lsof -ti:3000 | xargs kill; lsof -ti:5000 | xargs kill
Restart your laptop/server, it will release all the busy ports, then try again... you can also use
ps aux | grep node
and then kill the process using:
kill -9 PID..
You can kill all the processes that are using node it can also kill a system process
Not preferred: killall -9 node
but most of the times it wont work for nodemon, and didnt work for me.
You can fix this issue by killing the address in use or may simply restart your device.
1- For Linux to Kill the address in use, use the following command
sudo kill $(sudo lsof -t -i:8080)
where replace 8080 with your app address.
you can simply restart your laptop or change the port number it should work

Figure out which port a local Apache Spark UI is on

To find out some info about a local spark process, launched locally via spark-shell command I can do:
jps -lm | grep -i spark
However, how do I find what TCP port the UI is published on?
I have tried:
lsof -p PID
but no luck.
If you list open ports, you can filter by greping the PID (My pid is 30688, but you can also grep by java or something just to narrow the results):
$ ss -l -p -n | grep -i 30688
And, unless you've reconfigured it to a dramatically different range, you should be able to see a 40* port (I replaced tabs with -- to save space):
tcp--LISTEN--0--50--*:4040--*:*--users:(("java",pid=30688,fd=275))
Seems you already have the PID. in that case you can run below, which should indicate what ports the process is listening on:
netstat -tunlp|grep LISTEN|grep PID
The spark driver process may be listening on more than one ports, so you may have to try http://host:port for the ports obtained.
Alternatively, if you are finding only one PIDs from your jps | grep, then you can be sure that the port is 4040(default spark web ui port), unless you find the args spark.webui.port. In the latter case, get the port from the args value.

How to find out which port number a process is using and the process using a specific port number

I'm beginner with openhab, after launching the server /etc/init.d/openhab2 start i got that [ ok ] Starting openhab2 (via systemctl): openhab2.service, but I still have no access to the platform via http://localhost:8080/
I want to know :
which process is using port 8080
which port openhab runs on
thanks
you can use lsof,
lsof | grep TCP | grep 8080 and lsof | grep openhab | grep TCP
if a process forks and the child process is the one that is using a port it may not work as expected

Find pid of a process while its rinning on background

I have python sunning on my PC in a Linux machine .
ps -eaf | grep python
But now i dont know the process name say . Python is running on the port 7777. I only know the port no on which python with the bellow command .
netstat
Now i want to find out the pid no of python which is running on port 7777.
as i dont know the process name i only know port no 7777 . are there any command for the same problem .
You have to use following :
lsof -i :7777
I will show you pid without knowing the process name , but knowing the port no
sudo netstat -tunlp | grep :7777
You can use either netstat (deprecated) or ss, with the same options which are mnemonic:
-t = TCP
-u = UDP
-n = numeric output
-l = listening ports
-p = pid
Another commands that works, apart, from lsof, is fuser (the Linux one because the BSD's is different).
sudo fuser -n tcp -n 7777

Resources