Anand Vaidya
Anand Vaidya

Reputation: 649

Node.js/electron: How to Identify the process is windows process or other application process

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: enter image description here

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

enter image description here

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

Answers (1)

Estus Flask
Estus Flask

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

Related Questions