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][1] 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}`);
});
````
[1]: https://stackoverflow.com/users/3049109/chris-lam
Thanks to **Sidhharth Yadav** for this answer.
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][1] 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}`);
});
````
[1]: https://stackoverflow.com/users/3049109/chris-lam