Reputation: 649
I am working on an application which gives top 5 CPU usage applications names. Currently, I have got top 5 applications from the following code:
var _ = require('lodash');
var ps = require('current-processes');
ps.get(function(err, processes) {
var sorted = _.sortBy(processes, 'cpu');
var top5 = sorted.reverse().splice(0, 5); // Top 5 results
console.log(top5);
});
Output: Attaching o/p in image:
I have worked on other method as well:
var exec = require('child_process').exec;
exec('tasklist', function(err, stdout, stderr) {
var lines = stdout.toString().split('\n');
console.log(lines);
});
Output Image
But I am unable to identify whether the process(pid) is of windows services or other application. In short, I don't want to show any system service. Is there any other way to identify this?
Upvotes: 1
Views: 1495
Reputation: 223164
tasklist
is an acceptable way to do this.
System applications and services can be filtered out by Session Name
and Username
columns.
tasklist
helper package may be used instead of parsing command output manually.
Basically N/A
(can be localized) and NT AUTHORITY\*
(can have local names so NT AUTHORITY\SYSTEM
isn't reliable) users and services (likely fall under previous category) should be filtered out:
tasklist({ verbose: true })
.then(apps => {
apps = apps
.filter(app => app.sessionName !== 'Services')
.filter(app => /^(?!NT AUTHORITY).+\\/.test(app.username));
console.log(apps)
})
.catch(console.error);
Upvotes: 1