5

I am writing an extension for vscode, and I need to get the environment variables of a process that is already running. But I wasn't able to find a way to do it.

I know how to do it in python using psutil:

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?

psclkhoury
  • 155
  • 3
  • 10

5 Answers5

7

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.

Windows (Using powershell, 2019 is the PID )

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

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

Sudhakar Ramasamy
  • 1,736
  • 1
  • 8
  • 19
3

Combining with @SudhakarRS's answer:

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.

Replacing SomeProcess with notepad works for me, but using notepad.exe does not.

On powershell you can get the processes with a particular name using Get-Process [process name].

So, for example, if I have 4 instances of notepad running and do Get-Process notepad, I see this:

Processes

You can get the process IDs with (Get-Process notepad).Id which returns:

Image

You could use the same code to choose the 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:

Return

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.

user1274820
  • 7,786
  • 3
  • 37
  • 74
3

Easyish way(from here, you can use something like shelljs then run:

ps faux | grep 'PROCESS_NAME'

Then extract the process id(I'm just working on a regex) and then do:

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

You'll get the the env vars back as a string something like:

THEVAR=1
ANOTHERVAR=2

I reckon you just split the string by '\n' but I'm checking!

I'll update this once I figure the regex. **Are you on linux/mac or windows?

UPDATE: Check https://github.com/shelljs/shx for cross platform

Mrk Fldig
  • 4,244
  • 5
  • 33
  • 64
  • I am on Windows, but since this is an extension I would prefer if it works on all platforms with the same solution. – psclkhoury Oct 07 '19 at 17:01
  • Hmm ShellJS is supposed to be portable, I can't test it right now but will do asap. Feel free to accept any other relevant answers that solve your particular case, I'd suggest editing the title and description so it includes windows. – Mrk Fldig Oct 07 '19 at 17:11
  • Saying that you could combine these answers and just figure out which platform you're running on to run the right command! – Mrk Fldig Oct 07 '19 at 17:21
  • 1
    You want to split with `\r\n` (on windows at least ) – user1274820 Oct 07 '19 at 19:08
  • I'll might be able to test it beforehand, I'll do my best! – Mrk Fldig Oct 07 '19 at 19:09
0

There is no builtin way to do that in 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.

psclkhoury
  • 155
  • 3
  • 10
-1

yep:

process.env will give you what you need :)

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?

Gibor
  • 1,695
  • 6
  • 20