Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NODEJS process info

How to get the process name with a PID (Process ID) in Node.JS program, platform include Mac, Windows, Linux.

Does it has some node modules to do it?

like image 800
pianist829 Avatar asked Mar 18 '13 07:03

pianist829


People also ask

How do I find the process ID in Node JS?

To find the PID of the node process, you can either look in the pid file of in the log file.

What's process in Nodejs?

The process object in Node. js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node. js and process is one of them. It is an essential component in the Node.

What is process CWD ()?

process. cwd() returns the current working directory, i.e. the directory from which you invoked the node command. __dirname returns the directory name of the directory containing the JavaScript source code file.

How is process env set?

The process. env global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH variable set, this will be made accessible to you through process.


2 Answers

Yes, built-in/core modules process does this:

So, just say var process = require('process'); Then

To get PID (Process ID):

if (process.pid) {   console.log('This process is your pid ' + process.pid); } 

To get Platform information:

console.log('This platform is ' + process.platform); 

Note: You can only get to know the PID of child process or parent process.


Updated as per your requirements. (Tested On WINDOWS)
var exec = require('child_process').exec; var yourPID = '1444';  exec('tasklist', function(err, stdout, stderr) {      var lines = stdout.toString().split('\n');     var results = new Array();     lines.forEach(function(line) {         var parts = line.split('=');         parts.forEach(function(items){         if(items.toString().indexOf(yourPID) > -1){         console.log(items.toString().substring(0, items.toString().indexOf(yourPID)));          }         })      }); }); 

On Linux you can try something like:

var spawn = require('child_process').spawn,     cmdd = spawn('your_command'); //something like: 'man ps'  cmdd.stdout.on('data', function (data) {   console.log('' + data); }); cmdd.stderr.setEncoding('utf8'); cmdd.stderr.on('data', function (data) {   if (/^execvp\(\)/.test(data)) {     console.log('Failed to start child process.');   } }); 
like image 67
Amol M Kulkarni Avatar answered Oct 03 '22 02:10

Amol M Kulkarni


On Ubuntu Linux, I tried

var process = require('process'); but it gave error. 

I tried without importing any process module it worked

console.log('This process is your pid ' + process.pid); 

One more thing I noticed we can define name for the process using

process.title = 'node-chat'  

To check the nodejs process in bash shell using following command

ps -aux | grep node-chat 
like image 43
Vijay Rawat Avatar answered Oct 03 '22 02:10

Vijay Rawat