How to get the process details in node.js

Retrieving process details in node.js is easier than you think. We will be using ps-list package for this tutorial. If you don’t have ps-list already installed in your node.js, install it using the following command.

npm install ps-list

Retrieving all process details together as an object using psList()

//accessing ps-list module
const psList = require('ps-list');
//calling psList()
psList().then(data => {
  //printing returned object
    console.log(data);
});

the output for the above code snippet will contain an array, each of the elements of it will contain attributes like pid, name, cmd, cpu, etc. Sample output is given below (in windows)

[ { name: '[System Process]', pid: 0, ppid: 0 },
  { name: 'System', pid: 4, ppid: 0 },
  { name: 'Registry', pid: 96, ppid: 4 },
  { name: 'smss.exe', pid: 400, ppid: 4 },
  { name: 'csrss.exe', pid: 644, ppid: 632 },
  { name: 'wininit.exe', pid: 756, ppid: 632 },
  { name: 'services.exe', pid: 828, ppid: 756 },
  { name: 'lsass.exe', pid: 836, ppid: 756 },
  { name: 'fontdrvhost.exe', pid: 1000, ppid: 756 },
  { name: 'WUDFHost.exe', pid: 588, ppid: 828 },
  { name: 'svchost.exe', pid: 568, ppid: 828 },
  { name: 'dasHost.exe', pid: 1932, ppid: 1852 },
  { name: 'svchost.exe', pid: 1980, ppid: 828 },
  { name: 'svchost.exe', pid: 2436, ppid: 828 },
  { name: 'atiesrxx.exe', pid: 2528, ppid: 828 },
  { name: 'svchost.exe', pid: 2632, ppid: 828 },
  ... 216 more items ]

example 2: retrieving data from the returned object

//accessing ps-list module
const psList = require('ps-list');
//calling psList()
psList().then(data => {
  //walking through returned object
	data.forEach(function(element){
		console.log("name:"+String(element.name)+"  pid:"+String(element.pid));
  });	
});

output for the above code snippet

name:[System Process]  pid:0
name:System  pid:4
name:Registry  pid:96
name:smss.exe  pid:400
name:csrss.exe  pid:644
name:wininit.exe  pid:756
name:services.exe  pid:828
name:lsass.exe  pid:836
name:svchost.exe  pid:960
name:svchost.exe  pid:980
name:fontdrvhost.exe  pid:1000
name:WUDFHost.exe  pid:588
name:svchost.exe  pid:568
.
.
216 more

retrieving the priority of the process.

//allocating os module
const os = require('os');
//accessing ps-list module
const psList = require('ps-list');
//calling psList
psList().then(data => {
  //waling through returned object i.e. data
	data.forEach(function(element){
      //try catch for handling error from os.getPriority()
		try{
          //retrieving priority 
			var priority=os.getPriority(element.pid);
			console.log("name:"+String(element.name)+"  pid:"+String(element.pid)+" priority:"+String(priority));
		}catch(err){
          //a blank catch block
        //console.log(err);
      }
  });
});

output for the above code snippet

name:[System Process]  pid:0 priority:0
name:SynTPEnh.exe  pid:10376 priority:-7
name:sihost.exe  pid:9580 priority:0
name:ctfmon.exe  pid:13568 priority:-14
name:ShellExperienceHost.exe  pid:4032 priority:0
name:SynTPHelper.exe  pid:4216 priority:-7
name:SettingSyncHost.exe  pid:13748 priority:10
name:LockApp.exe  pid:3244 priority:0
name:CastSrv.exe  pid:5364 priority:0
name:RuntimeBroker.exe  pid:3292 priority:0
name:WindowsInternal.ComposableShell.Experiences.TextInput.InputApp.exe  pid:10884 priority:0
name:GameBar.exe  pid:12732 priority:0
name:RuntimeBroker.exe  pid:11400 priority:0
name:GameBarFT.exe  pid:3904 priority:0
name:RuntimeBroker.exe  pid:13264 priority:0
name:Video.UI.exe  pid:12736 priority:0
name:RuntimeBroker.exe  pid:9624 priority:0
name:Calculator.exe  pid:13004 priority:0
name:chrome.exe  pid:4336 priority:0
name:chrome.exe  pid:5312 priority:-7
name:chrome.exe  pid:9704 priority:0
name:chrome.exe  pid:13688 priority:19
name:chrome.exe  pid:6636 priority:0
name:audiodg.exe  pid:7524 priority:0
name:CompPkgSrv.exe  pid:3336 priority:0
name:notepad++.exe  pid:1072 priority:0
name:cmd.exe  pid:2804 priority:0
name:conhost.exe  pid:14256 priority:0
name:chrome.exe  pid:3288 priority:19
name:RuntimeBroker.exe  pid:6260 priority:0
name:node.exe  pid:14284 priority:0

If you need any other help related to this topic then don’t hesitate to write in the comment below.

You may also like...

Leave a Reply

Your email address will not be published.