NODEJS-procesinfo

Hoe de procesnaam te krijgen met een PID(proces-ID) in het Node.JS-programma, platform omvat Mac, Windows, Linux.

Heeft het een aantal knooppuntmodules om het te doen?


Antwoord 1, autoriteit 100%

Ja, ingebouwde/kernmodules processdoet dit:

Dus zeg gewoon var process = require('process');Dan

PID (proces-ID) verkrijgen:

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

Platforminformatie verkrijgen:

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

Opmerking:u kunt alleen de PID van het onderliggende proces of het bovenliggende proces leren kennen.


Bijgewerkt volgens uw vereisten. (Getest op 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)));
         }
        }) 
    });
});

Op Linuxkun je zoiets proberen als:

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.');
  }
});

Antwoord 2, autoriteit 18%

Op Ubuntu Linux heb ik het geprobeerd

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

Ik heb het geprobeerd zonder een procesmodule te importeren, het werkte

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

Nog iets wat me opviel: we kunnen de naam voor het proces definiëren met

process.title = 'node-chat' 

Om het nodejs-proces in bash-shell te controleren met het volgende commando

ps -aux | grep node-chat

Antwoord 3, autoriteit 7%

zie officiële documentatie https://nodejs.org /dist/latest-v10.x/docs/api/process.html#process_process_pid

de vereisteis niet meer nodig.
Het goede voorbeeld is:

console.log(`This process is pid ${process.pid}`); 

Other episodes