How to run Linux/Ubuntu commands(not executables) from Nodejs - node.js

I have been running executables using spawn in nodejs all this while, now when i am trying to use spawn to run ubuntu commands like unset, export etc, they dont seem to work. I guess cause it is looking for executables.
I even tried exec, that does not seem to work too. Any tips?
I have an ubuntu device running node, from the UI i need to set/unset env variables for proxy, e.g. http_proxy and no_proxy. Apart from export what other way can i do it via node? The env variables should be set system wide not just for the current process.

Environment variables only exist in memory and are local to a process. For any running process, only the process itself can make changes to the set of environment variables "belonging" to that process, but those changes will not propagate to existing child or parent processes.
In short: you cannot change an environment variable that will apply to all processes on your system (not even from a regular shell).
You can only set an environment variable so it becomes available for newly created child processes (child processes by default inherit the set of environment variables from their parent), but that's about it.
If you have control over how the processes that require those specific environment variables are started, you could write the value for those variables to a file (from Node) and source that file right before the other processes are started, but it really depends on the actual use case whether this is a usable option.

Related

Is it possible to set the system proxy from golang app in macOS or Linux distributes?

I'm creating golang app with a proxy server as a pet project, and I want to have the possibility to set himself as a system proxy. Is it possible? And how?
Proxies are often configured through the HTTP_PROXY, HTTPS_PROXY and NO_PROXY environment variables. In Unix-style operating systems, these variables are inherited from the parent process to its child processes.
This being said:
your program can modify its own environment and start other processes as child processes (passing in the _PROXY variables).
your program may modify the shell startup scripts (like .bashrc, .cshrc), and a newly started shell will pick the values up and pass them to newly started processes.
But I am not aware that modifying the environment of other (active) processes is possible.

How can I reset the environment to the default for a user when running program as that user?

I'm trying to run a program in Rust using libc as another user.
nix::unistd::setuid(uid).unwrap();
If I fork and exec a process which runs printenv, the environment does not change. How can I reset the environment to the default for a user?
With nix::unistd::execve()'s third argument env, you have full control over the next processes environment. Also see exec(3).

Change ENV variables at runtime

Is it possible to inject/change the current enviroment variables in an already loaded and started NodeJS process?
Exposing an Interface within the application is not an option, restarting is also not a valid option.
The process is running inside a docker container, requiring a specific NodeJS Version is possible.
EDIT: The change must be done from outside the application source so doing process.env.ENV_VAR = "new env" is not possible.
It isn't possible to modify the env vars of a running process. This isn't unique to NodeJS processes. It's just how env vars work on UNIX like operating systems. The vars live within the address space of the process. And while they are, typically, initially placed at a well known location near the top of the stack the current vars are likely to be at an arbitrary address in the heap. Env vars are intentionally private to each process. So unless the program provides an API for changing its env vars you can't modify them once the program is running.
You should use a redis store shared between containers that has stored the env.
redis node repo - redis listen for changes

Get environment variables of a running process

Where or how can I get the environment variables of a running process?
Consider the case that I'm neither root nor the user that started the process.

Set PATH for all users AND processes

How do I set the PATH variable for all users and processes? I currently only have the PATH set in /etc/profile but this is obviously not loaded when I run a process remotely. I am running SaltStack and the minion process, when called from the master does not have the right path. Is there a way to set the PATH in a single place for all users and processes?
It depends on which distribution you are using.
On debian/ubuntu the default PATH variables are at:
/etc/login.defs
Also, every process has a process that calls it and it will inherit the PATH from that process. So it doesn't make sense to set a global process PATH that is distinct from the user PATH.
Not an answer to your specific question, but try added 'shell: /bin/bash' and 'runas: username' to your cmd state. It will then inherit the default login path for that user.

Resources