How to find which Node.js file is running now - node.js

I forgot what node.js file now running on port 3000. How can I know it?
I executed the command
lsof -c node
but it did not help me find out the name of the file.
(Linux OS)

ps auxww | grep node should do the trick.

Related

Why HTTP ports stay open when using them by Nodejs servers?

I have a problem when launching a Nodejs script that listens in one of the HTTP ports. Sometimes, even if I stop the script, the used HTTP port stays "in use", making it impossible to use it another time. Today, i've set up NGINX in my linux and all the HTTP ports were "in use". I was obliged to restart my computer to solve the problem.
I wanted to know why is this happening ? What can i do to prevent it ? and in case an HTTP port stays "in use", how can i close it to be able to use again ?
Thanks for your help.
This is applicable only on Linux and MacOS, you can list all your used ports like that:
sudo lsof -i -P -n | grep LISTEN
Read more here about how to check if a port is in use: https://www.cyberciti.biz/faq/unix-linux-check-if-port-is-in-use-command/
You can also list the node processes:
top | grep node
or
ps -ef | grep node
Then you can kill the node processes like that:
killall node
Make sure that when you want to stop the server you are pressing CTRL + C

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

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

How can I tell on CentOS how many node programs are currently running?

If I have node on my machine, and I run four different node programs, is there a way for me to tell from the CLI how many programs are running?
It's usefull, I think, when I need to tell if a specific node program is on, or just to know how many programs node is running.
Thanks!
Well, you can always run:
$ ps -aef | grep node
If you're only bothered about how many:
$ ps aux | grep `which node` | wc -l
And if you actually want to see details of them, just lop the wordcount (wc -l( command off the end:
$ ps aux | grep `which node`
NB: Using which node will return the location of whichever node executable is to be run by that shell, at that time - this may differ if you run it in a script somewhere, versus at your own terminal prompt, and it may differ a lot if you use NVM. If you have multiple node binaries, you could use grep node but be aware that this may find other processes which include the word node, and sometimes there are many - on a dev machine it will find running versions of Slack, and other desktop apps that use node

distinct several services nodejs with ps aux | grep node

I have 6 services nodejs running in one server. Sometimes I need to kill one of them, but when I execute:
ps aux | grep node
All the 6 services appears with the same name. I dont know which one of them to kill.
There are some way to diff. them?
You can use PM2 to manage your nodejs applications. It can also help you in restarting the node process once server is rebooted.
If you know the port of your services and linux, this could help:
sudo netstat -tulpn

Node Restart in Shell Script

I want to rerun the node server, whenever there's changes in file, but i want to use fswatch. i am using fswatch with the shell script like
nohup node server.js &
but i can't run the same script again since it will throw EADDRINUSE.
whats the best way to restart the node via script with the help of fswatch (or alternate, without any new installation) ?
Why node doesn't have something like node restart?
what i could think of is get pid from ps and kill it with script, but i guess there should be a better solution.
I am able to do that with the help of fswatch.
fswatch -o mydir | xargs -n1 -I{} ps | grep '[n]ode server.js$' | awk '{system("kill "$1)}'
or putting the same code in seperate shell file. and using it as
xargs -n1 './location-of-shell-file.sh'
when i run grep, that process is also included in ps, so to exclude that i had to use
grep '[n]ode server.js'
EADDRINUSE comes because of you are already bind on the same port. Node.js has no build-in restart mechanics, so yes, you should use some bash scripts (or frameworks) for good restarting background app

Resources