NODEJS 进程信息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15471555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NODEJS process info
提问by pianist829
How to get the process name with a PID(Process ID) in Node.JS program, platform include Mac, Windows, Linux.
如何PID在Node.JS程序中获取带有(Process ID)的进程名,平台包括Mac、Windows、Linux。
Does it has some node modules to do it?
它是否有一些节点模块可以做到这一点?
回答by Amol M Kulkarni
Yes, built-in/core modules processdoes this:
是的,内置/核心模块process是这样做的:
So, just say var process = require('process');Then
所以,就说var process = require('process');然后
To get PID (Process ID):
获取PID(进程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.
注意:只能知道子进程或父进程的PID。
根据您的要求更新。(已测试
WINDOWSWINDOWS)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 Linuxyou can try something like:
在Linux你可以尝试这样的:
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.');
}
});
回答by Vijay Rawat
On Ubuntu Linux, I tried
在 Ubuntu Linux 上,我试过
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
使用以下命令检查 bash shell 中的 nodejs 进程
ps -aux | grep node-chat
回答by Didier68
cf official documentation https://nodejs.org/dist/latest-v10.x/docs/api/process.html#process_process_pid
参见官方文档 https://nodejs.org/dist/latest-v10.x/docs/api/process.html#process_process_pid
the requireis no more needed. The good sample is :
该要求是没有更多的需要。好的样本是:
console.log(`This process is pid ${process.pid}`);

