简体   繁体   English

Nodejs - 获取正在运行的进程的环境变量

[英]Nodejs - Get environment variables of running process

I am writing an extension for vscode, and I need to get the environment variables of a process that is already running.我正在为 vscode 写一个扩展,我需要获取一个已经在运行的进程的环境变量。 But I wasn't able to find a way to do it.但我无法找到一种方法来做到这一点。

I know how to do it in python using psutil:我知道如何使用 psutil 在 python 中做到这一点:

for proc in psutil.process_iter(attrs=['name', 'exe']):
    if proc.info['name'].lower() == 'SomeProcess.exe'.lower():
        return proc.environ()

Is there something similar for javascript/nodejs? javascript/nodejs 有类似的东西吗?

You can use child_process module to spawn a terminal and execute the following commands wrt platform and get the variables, parse & use or write a native node module to access the proper APIs of each platform and get the output.您可以使用 child_process 模块生成终端并在平台上执行以下命令并获取变量,解析并使用或编写本机节点模块以访问每个平台的适当 API 并获取 output。

Windows (Using powershell, 2019 is the PID ) Windows(使用powershell,2019为PID)

(Get-Process -id 2019).StartInfo.EnvironmentVariables

Linux Linux

tr '\0' '\n' < /proc/2019/environ

Mac苹果电脑

ps eww -o command 2019  | tr ' ' '\n'

Thanks to https://serverfault.com/a/66366 & https://stackoverflow.com/a/28193753/12167785 & https://apple.stackexchange.com/a/254253 & https://stackoverflow.com/a/11547409/12167785 & https://stackoverflow.com/a/18765553/12167785 Thanks to https://serverfault.com/a/66366 & https://stackoverflow.com/a/28193753/12167785 & https://apple.stackexchange.com/a/254253 & https://stackoverflow.com/ a/11547409/12167785 & https://stackoverflow.com/a/18765553/12167785

Combining with @SudhakarRS's answer:结合@SudhakarRS 的回答:

var child = require('child_process').execFile('powershell', [ 
    '(Get-Process SomeProcess).StartInfo.EnvironmentVariables' 
], function(err, stdout, stderr) { 
    console.log(stdout);
}); 

If you want to debug it, make sure you peek at err and stderr .如果要调试它,请确保查看errstderr

Replacing SomeProcess with notepad works for me, but using notepad.exe does not.notepad替换SomeProcess对我有用,但使用notepad.exe不行。

On powershell you can get the processes with a particular name using Get-Process [process name] .在 powershell 上,您可以使用Get-Process [process name]具有特定名称的进程。

So, for example, if I have 4 instances of notepad running and do Get-Process notepad , I see this:因此,例如,如果我运行 4 个记事本实例并执行Get-Process notepad ,我会看到:

流程

You can get the process IDs with (Get-Process notepad).Id which returns:您可以使用(Get-Process notepad).Id进程 ID,它返回:

图片

You could use the same code to choose the ID:您可以使用相同的代码来选择 ID:

var child = require('child_process').execFile(
    'powershell',
    ['(Get-Process notepad).Id'],
    function(err, stdout, stderr) { 
        var ids = stdout.split("\r\n");
        ids.pop(); //remove the blank string at the end
        console.log(ids);
    }
);

^ which returns: ^ 返回:

返回

If you just want to grab the first process with a name, it's:如果您只想获取具有名称的第一个进程,它是:

(Get-Process notepad)[0].StartInfo.EnvironmentVariables

^ obviously replace notepad with your process name. ^ 显然用您的进程名称替换notepad

Easyish way(from here , you can use something like shelljs then run:简单的方式(从这里,你可以使用shelljs 之类的东西然后运行:

ps faux | grep 'PROCESS_NAME'

Then extract the process id(I'm just working on a regex) and then do:然后提取进程ID(我只是在处理正则表达式),然后执行:

cat /proc/THE_PROCESS/environ | tr '\0' '\n'

You'll get the the env vars back as a string something like:您将把 env vars 作为字符串返回,例如:

THEVAR=1
ANOTHERVAR=2

I reckon you just split the string by '\n' but I'm checking!我想你只是用 '\n' 分割字符串,但我正在检查!

I'll update this once I figure the regex.一旦我确定了正则表达式,我会更新它。 **Are you on linux/mac or windows? **您使用的是 linux/mac 还是 windows?

UPDATE: Check https://github.com/shelljs/shx for cross platform更新:跨平台检查https://github.com/shelljs/shx

yep:是的:

process.env will give you what you need:) process.env会给你你所需要的:)

you can read some more here .你可以在这里阅读更多内容。

EDIT : it will give you environment variables only for the process you're in ... did I misunderstood and you want varibales of another process?编辑:它只会为您所在的进程提供环境变量......我是否误解了,您想要另一个进程的变量?

There is no builtin way to do that in javascript/nodejs.在 javascript/nodejs 中没有内置的方法可以做到这一点。 If you really need to do it, then the best way is to run a command in the terminal and then parse the output to construct the object that you need.如果你真的需要这样做,那么最好的方法是在终端中运行一个命令,然后解析 output 来构造你需要的 object。

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

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