distinct several services nodejs with ps aux | grep node - node.js

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

Related

Is it possible to connect to running NestJS server stderr/stdout?

I'm not sure if it's possible, but I want to connect to existing application to see what logs it produce in realtime. Is it possible to do so from Linux terminal?
If you know the pid of the process (it should be in the start up logs, or retrieved via a ps aux | grep <port> call) you should be able to tail it via tail -f /proc/<pid>/fd/2. Command found from here

Not able to find port number for tomcat process on Ubuntu

I am not able to find a port number for running Tomcat server on Ubuntu
i.e. netstart -anp | grep 'tomcat' but not getting any output.
You will want to grep for java and not tomcat, as the process binary is java and not Tomcat.
If you'd like to find the PID for the process to make things easier (e.g. if you have lots of Java processes on the server), you can do this:
ps aux | grep catalina
This will show you your various Tomcat processes. Each one will have a system property on the command-line like this:
-Dcatalina.base=/path/to/your/tomcat
Note that there is also catalina.home which may be different. If they are different, it is catalina.base which is the correct one, which contains your server's conf/server.xml which controls the server.
Once you have that PID, you can netstat -plan | grep [PID] to get your port number.
Or just look in /path/to/your/tomcat/conf/server.xml for any <Connector> elements, each of which should have a port specified.

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

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

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

Resources