Ruby: Setting environment variables using system() - ruby

I am creating the following method which sets my environment variables:
def set_time
puts "setting_time"
system("export GIT_COMMITTER_DATE='#{#date}'")
system("export GIT_AUTHOR_DATE='#{#date}'")
end
But for some reason when I go into my console after running this, the environment variables have not been added! I am able to run other command line interface keywords and it works. How can I set environment variables from Ruby?

The reason they don't 'stick' is because when you run a shell command from Ruby it opens a new process. And that process, as a child to your current Ruby process is not persistent, as eventually it dies.
A good resource on this is Jesse Storimer's blog post, with much more information about environment and processes than I will type here.
Depending on your operating system, you can use Ruby to write to your 'rc' files, such as ~/.bashrc or on Windows change your registry, if you really want to have these be persistent over logins.
So the answer is, you ARE setting and exporting environment variables. They simply aren't surviving past the life of the child process.

Related

How to make PowerShell recognize changes in environment variables?

When I change any environment variable in Windows using GUI (System Properties/Environment Variables/System Variables) changes are reflected immediately in cmd terminal, but PowerShell shows an old value.
Here cmd recognized that I changed JAVA_HOME value from C:\Users\nikif\.jdks\openjdk-19.0.1 to C:\Users\nikif\.jdks\corretto-16.0.2
PowerShell still shows the previous value - C:\Users\nikif\.jdks\openjdk-19.0.1
Is there any command that will make PowerShell recognize the environment variable change made in GUI?
Or maybe there is a better way to change the environment variable from PowerShell?
Thank you.
This all should really be a comment (I tried above), but attempting to explain why the question, as it stands, is too vage requires just more explaination.
Processes get their environment when started by the OS. They basically get a copy the environment as it was at that point in time. When you start a process you can (using the respective APIs) pass additional or altered environment variables.
During the existance (runtime) of a process you cannot change the environment.
Shells are a common exception, as they provide specific syntax that does that (set NAME=VALUE in CMD.EXE and $env:NAME="VALUE" in PowerShell, for example). An application itself could also do such stuff from its code using respective APIs. But in general, there is no way to change variables from the outside.
One process will never see the changes done to the environment in a different process. For example, if you have to separate CMD.EXE sessions running and change a variable in one of them, the other will not know. The environment is private to each process.
A potential exception is the (global/system) environment variables you can set using the Computer/Properties/Environment setting (applet). The system will send a WM_SETTINGCHANGE window message indicating the that environment has changed. Applications can register to this message and act accordingly.
However, neither PowerShell nor CMD.EXE do seem to listen to this message (it would require a (hidden) Window anyway and both are console applications). And frankly, it would be not good if they did. Consider CMD.EXE just execuing a batch file and a variable (say PATH) changes underneath - security issues and general havoc all over.

change environment variable on a running process in mac

I have a running process on mac and I want to change is environment variable from out side the process, using some command line utility.
How can I do that?
You cannot change the environment variables of a running process via an external utility. Doing so would require the utility to modify the address space of the process. Note that this is not a limitation of macOS. This is a limitation of the UNIX process model. When a UNIX process is created by the kernel the environment variables are put in its address space; typically near the top of the stack. They are not stored in the kernel data structures for the process. And thus there are no system calls for getting or setting those vars. Which means there is no way for one process to affect the env vars of a second process other than at the time the second process is created via execve() or a related syscall.

Electron get or set Environment variables in Windows?

I know that from JavaScript it is not possible to get or set Windows Environment Variables. I have deployed a Electron app as a executable (as suggested here), is it possible now to get or modify a Windows Enviroment Variable. If it is can someone point me in the right direction ?
You can use NodeJs to read the enviroment variables via process.env you can read all methods and possabilities on the process documentation.
Example:
console.log(process.env.PATH)
Writing environment variables in a running process is always temporary. You can set environment variables by calling/spawning system commands/tools like setx under windows.
Persisting them does not change the running environment in the process for that you have to restart the process. So you need both, set the variable as described above and in addition persist it with system tools/commands.

OS X: Attach to a running process and read its environment variables

Is it possible (and if so, how?) to read a running process's environment variables. To be more specific, I am concerned with environment variables set by the process itself (using setenv() or similar), not the general user session environment variables set when the process is launched.
I figured that DTrace would be the tool for this job, but the only solution I've found online (from this mailing list thread) appears to be specific to Solaris because the script fails to execute on my Mac.
Perhaps Instruments exposes this functionality somehow and I just haven't found it yet?
I have also tried Tasks Explorer, which is a nice little application, and it shows process info including environment variables, but it does not show any environment variables set by the process itself, which is what I'm after.
Any help would be much appreciated!
I suppose you could try next way:
Inject you dylib into running process.
To read environment variables from injected dylib.
Via IPC (RPC, Shared Memory, etc.) send Environment variable into you application.
By the way, I have a plan to add this type of environment variables exploring into my Tasks Explorer.
Next links will help you with injection:
https://github.com/comex/inject_and_interpose
SIMBL/Bundle/dylib injection into Dock.app

How to modify the PATH variable definitely through the command line in Windows

I would like to make a .bat file that would add some string at the end of the value of the Windows PATH variable. Warning, I want this change to be definitive, not working only for the current session.
Does somebody know of a way to do this ? As much as possible it should not be dependent on the version of Windows
Sorry for the long answer, but a short answer on your question is impossible.
First of all you should understand how environment variables works. There are some places in the registry like HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment where the environment variables will be hold. On start-up the operation system reads this registry keys. Then one windows process creates another windows process. The parent process can give to the client process any set of environment variables. If the parent process doesn't do this, the child process inherits environment variables of the parent processes.
To be able update environment variables of a running process with respect of WM_WININICHANGE or WM_SETTINGCHANGE messages. A windows application can interpret this messages and reread the current environment variables from the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment. So you can in general change registry values under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment or HKEY_CURRENT_USER\Environment and send
SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment");
It would be much better to use SendMessageTimeout instead of SendMessage, but the idea will stay the same. The problem is that other processes must not wait for the message and do something. Most console application have no message loop and don't do anything if you send such messages.
So it is important to understand that there is no simple way to update environment variables of all processes without restarting of the computer. You should have a clear understanding of this and reduce your question a little.
If you update the environment in registry and send SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment") then new processed created by Explorer.exe will be have new environment variables, but cmd.exe will not do this.
If you want to update environment variables of the current cmd.exe inside a batch run you can do the following: You can create a new CMD file for example t.cmd in %TEMP% directory, write in the file SET PATH=%PATH%;C:\BlaBla and then use call %TEMP%\t.cmd and dell %TEMP%\t.cmd to update environment variables of the current cmd.exe.
To be exactly there are more places which are used to build environment variables of new created processes. This are subkeys of the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths and %SystemRoot%\System32\autoexec.nt file. One will be used for processes created by ShellExecute and ShellExecuteEx (for example Explorer.exe) and another for console applications.
If you only care about new process instances, and you really want it to be done via a batch file, then setx is what you're looking for.
/M will change the PATH in HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER.
i.e. a system variable, instead of user's.
example: SETX /M PATH "%PATH%;C:\your path with spaces"
If you want to directly change an environment variable for currently running processes, well, yeah, that's complicated and apparently not recommended:
Altering the environment variables of a child process during process creation
is the only way one process can directly change the environment variables of
another process. A process can never directly change the environment variables
of another process that is not a child of that process.
Otherwise, like Oleg says, programmatically the best way is to change the registry and send WM_SETTINGCHANGE and hope the apps are nice enough to pick it up.

Resources