简体   繁体   English

在Node JS中杀死进程之前检查进程所有者名称

[英]Check the process owner name before killing process in Node JS

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# ) 与 C# 类似

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 .为此,您可以使用此节点包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.一旦你得到了,你可以从进程列表中获取uids ,然后将它与从你正在执行的地方运行节点进程的当前用户匹配。 To get the uid of the user currently running the node process, you can write this:要获取当前运行节点进程的用户的uid ,可以这样写:

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 .一旦您获得了与当前用户匹配的 uid 的进程,您就可以使用此代码终止这些进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM