Как получить список процессов
Я играю с узлом и просто установил его на свою машину. Теперь я хочу получить список процессов, запущенных на моей машине, чтобы я мог видеть, работает ли Apache, MySQL запущен и т.д.? Как я могу это сделать? У меня просто очень простой код в моем файле JS. Я даже не знаю, с чего начать.
Вот мой код:
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200);
response.write("Hello world");
console.log('Listenning on port 1339');
response.end();
}).listen(8080);
Ответы
Ответ 1
Насколько я знаю, для этого кросс-платформенный модуль еще не существует. Вы можете использовать API-интерфейс дочернего процесса для запуска инструментов, которые предоставят нужные вам данные. Для Windows просто запустите встроенный процесс списка задач.
var exec = require('child_process').exec;
exec('tasklist', function(err, stdout, stderr) {
// stdout is a string containing the output of the command.
// parse it and look for the apache and mysql processes.
});
Ответ 2
См. ps-node
Чтобы получить список процессов в node:
var ps = require('ps-node');
ps.lookup({
command: 'node',
arguments: '--debug',
}, function(err, resultList ) {
if (err) {
throw new Error( err );
}
resultList.forEach(function( process ){
if( process ){
console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
}
});
});
Ответ 3
Вы также можете использовать текущие процессы, в которых перечислены все процессы.
https://www.npmjs.com/package/current-processes
Результат включает имя, pid, cpu и память, используемое процессом.
Вы также можете отсортировать результат и ограничить количество процессов.
Результат выглядит следующим образом:
[ Process {
pid: 31834,
name: 'atom',
cpu: 84,
mem: { private: 19942400, virtual: 2048, usage: 0.4810941724241514 } }]
Ответ 4
ps-list является лучшим пакетом node для работы, он также работает на платформах Linux, BSD и Windows.
const psList = require('ps-list');
psList().then(data => {
console.log(data);
//=> [{pid: 3213, name: 'node', cmd: 'node test.js', cpu: '0.1'}, ...]
});
Ответ 5
Кажется, нет никаких прямых методов
но ниже видео может помочь.
Ответ 6
Решение для unix-подобных систем:
const child_process = require('child_process');
const displayProcessBy = (pattern) => {
let command = `ps -aux | grep ${pattern}`;
child_process.exec(command, (err, stdout, stdin) => {
if (err) throw err;
console.log(stdout);
});
}
Примеры использования и результатов
displayProcessBy ( "nodejs" );
setivol+ 7912 0.0 0.0 12732 2108 ? S 10:56 0:00 grep nodejs
setivol+ 12427 0.0 0.0 669552 712 pts/3 Tl Dec16 0:00 nodejs
setivol+ 14400 0.0 0.0 669552 644 pts/2 Tl Dec15 0:00 nodejs
setivol+ 14412 0.0 0.0 670576 224 pts/3 Tl Dec16 0:00 nodejs
setivol+ 14567 0.0 0.0 669552 436 pts/3 Tl Dec15 0:00 nodejs
setivol+ 14911 0.0 0.0 669552 0 pts/3 Tl Dec15 0:00 nodejs
setivol+ 15489 0.0 0.0 669552 712 pts/3 Tl Dec16 0:00 nodejs
setivol+ 15659 0.0 0.0 669520 0 pts/3 Tl Dec16 0:00 nodejs --harmony
setivol+ 16469 0.0 0.0 669520 704 pts/3 Tl Dec16 0:00 nodejs --harmony
setivol+ 20514 0.0 0.0 669552 664 pts/2 Tl Dec15 0:00 nodejs
displayProcessBy ( "python2" )
setivol+ 8012 0.0 0.0 4336 712 ? S 10:58 0:00 /bin/sh -c ps -aux | grep python2
setivol+ 8014 0.0 0.0 12728 2240 ? S 10:58 0:00 grep python2
среда тестирования
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie