1

Does any Node library provide an option to get the owner's name? This is to avoid killing the processes of users in the case of a multiuser system.

Using the below code snippet to kill processes.

                    const find = require('find-process');

                    find('name', 'Teams', true).then(function (list) {
                    var i;
                    for (i = 0; i < list.length; i++) {
                        //logger.info(list[i].pid);
                        process.kill(list[i].pid);

                      }
                    });

Does it ensure to return processes of the current user or check username check is required before killing it?

How can the process owner's name be obtained? (As in Similar to C# )

Added on: 3rd August 2021.

Now I see that there are some packages available but restricted to certain OS versions. In this case, I am looking for some way in Windows 10.

  1. Owner name and/or uid of the logged-in user and process
  2. Owner name and/or uid of the process to be killed (Whichever the package which returns the required details for the process list)
Vaibhav More
  • 212
  • 1
  • 14

1 Answers1

0

First of all you need to get the list of process running on the system. For that you can use this node package ps-list.

To get the list you can write this code:

const psList = require('ps-list');
(async () => {
    console.log(await psList());
    //=> [{pid: 3213, name: 'node', cmd: 'node test.js', ppid: 1, uid: 501, cpu: 0.1, memory: 1.5}, …]
})();

Once you do get that you can get the uids from the process list and then match it with the current user running the node process from where you are executing. To get the uid of the user currently running the node process, you can write this:

const os=require("os");
const userInfo=os.userInfo();
console.log(userInfo.uid);

Once you get the process that match the uid with the current user you can kill those process with this code.

Pronoy999
  • 645
  • 6
  • 25
  • Ps-List code doesn't seem to be working in the window and throws an error. Also, userInfo always returns -1 as uid. – Vaibhav More Jul 30 '21 at 09:42