1

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?

Anand Vaidya
  • 609
  • 2
  • 16
  • 41

1 Answers1

1

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);
Estus Flask
  • 206,104
  • 70
  • 425
  • 565
  • I am a little bit confused where should I put these things? really apologized for such comment, but I am new to this thing and unable to get it. Can you please add my above code as well to explain this? – Anand Vaidya May 28 '18 at 11:45
  • As u say. I am also looking at the session. If it is a system application then it is 0 else it is 4. Can we use it for distinguishing? – Anand Vaidya May 28 '18 at 11:46
  • You should put this to the place where you call the code. `await` should occur inside `async` function. If you don't have one, you can wrap the code with IIFE: `(async () => { ... })().catch(console.error)` or switch to regular promises. I guess `Services` session name corresponds to 0 session. I expect it to be same regardless of locale, but you can use it for safety, too. I'd suggest to address OS-specific questions at https://superuser.com/ if you have them. – Estus Flask May 28 '18 at 11:52
  • I am getting errors Can you please add my code with yours and edit the answer? I am not using 'await'. – Anand Vaidya May 28 '18 at 11:58
  • I edited it. Surely not. tasklist is Windows-specific, the question was about *windows process*. If you're after other OSes, you need to handle them separately. Check https://github.com/sindresorhus/ps-list/blob/master/index.js for how it's done. Basically it's tasklist for windows and ps for mac and linux. You can use a helper like https://www.npmjs.com/package/ps to avoid parsing `ps` output manually. – Estus Flask May 28 '18 at 12:04
  • Thank you @estus – Anand Vaidya May 28 '18 at 12:06
  • is this memUsage is in bytes ? do you know how to get overall memory? – Anand Vaidya May 28 '18 at 12:39
  • Yes, it's in bytes, see https://www.npmjs.com/package/tasklist for reference. By overall you mean free/used machine RAM? It's not related to processes because it's not equal to a sum of their used memory. See https://stackoverflow.com/a/33145420/3731501 – Estus Flask May 28 '18 at 13:04
  • Thanks, I got it. – Anand Vaidya May 28 '18 at 13:09
  • tasklist provides "imageName" is there any way to get app Name. Example: imageName= chrome.exe and name = chrome. Is there any way to do it? – Anand Vaidya May 29 '18 at 06:41
  • I believe it's windowTitle field. Consider asking at superuser.com, this is specific to `tasklist` command - and `ps` if you're trying to support *nix). I'm not proficient with them. – Estus Flask May 29 '18 at 06:45
  • Sure, let me check it. – Anand Vaidya May 29 '18 at 06:48