4

Microsoft has a knowledge base article, last reviewed in June 2007, which states:

If you update environment variables or add new environment variables, you must restart the computer before the changes that you make affect services that run under the Local System account.

It's unclear to me whether this holds true for Windows Server 2008 R2. Can anyone advise?

I notice there is a hotfix for Windows 2003 that solves this problem. Consequently I wonder if 2008 R2 has rolled in this fix. I can't find any evidence to suggest there is still a problem with Windows 2008 R2, but a lack of evidence is less comforting than a concrete statement to the contrary.

A related question covers similar ground, but doesn't discuss Windows 2008.

Duncan Jones
  • 680
  • 1
  • 6
  • 17

4 Answers4

8

Restarting a service will pickup changes to system environment variables.

To check: Use SysInternals Process Explorer to inspect the environment of a service process before and after changing/adding a System environment variable and restarting the service. Note: Use "Run as administrator" on procexp.exe to see all properties of system processes.

enter image description here

enter image description here

Brian
  • 3,386
  • 17
  • 16
  • This. If a new process is spawned under the local 'System' account, said new process gets a new environment (w/ any modifications made). Hence, restarting a service will re-read the environment. – Signal15 Nov 19 '14 at 21:13
  • I can confirm by direct experiment that restarting a service causes a re-read of system environment variables, on Windows 7. This works for IIS and its w3wp worker processes. It also works for non-LocalSystem services, such as STISVC. – William Jan 19 '16 at 00:00
  • procexp was a great tip. On the Win10 Pro build 19042 system I'm using, restarting the jenkinsslave-C__jenkins service *didn't* get a fresh environment, just inherited the stale one from services.exe. Reboot it is, then. Yeah, that worked. – Martin Dorey Apr 28 '22 at 23:24
1

Sure. Any process sees the environment variables AS THEY ARE WHEN THE PROCESS STARTS. It basically gets a copy.

So, if you need them to be picked up by services - restart the services. If you need them to be picked up by windows, restart windows.

Especially because the local system account is keeping this copy around ;) So - no. This still holds true. I would generally say that changing this is a "near zero priority" item as this kind of change is quite rare.

TomTom
  • 50,857
  • 7
  • 52
  • 134
  • I found the last paragraph a little confusing - in summary, you think this still applies to Windows 2008 and a reboot is required? If so, do you have any evidence to support that claim? I notice that [a hotfix](http://support.microsoft.com/kb/887693) was available for Windows 2003 that resolves the issue - are you sure this isn't incorporated into 2008 R2? – Duncan Jones Nov 11 '14 at 10:42
  • In the general case, I would expect a process to inherit its environment variables from its parent, rather than getting a fresh copy of Windows's configuration. I can imagine that services are supposed to be special, but it doesn't seem true for me today (see above), so I certainly don't think that it's self-evident enough to warrant teh shouty. – Martin Dorey Apr 28 '22 at 23:24
  • But it DOES - it inherits them AS THEY ARE WHEN THE PROCESS STARTS. Note that this goes down to another issue: Many (most) programs are not prepared for environment variables CHANGING while the process is running. Like you put in an environment variable where the temp folder is - the program does not expect this to change during it's runtime. Hence "at the start of the process". Then a program (process) starts, it gets a copy of the environment variables AS THEY ARE AT THIS MOMENT. – TomTom Apr 29 '22 at 08:13
1

@Brian answer does not cover all use cases. In my case, I've upgraded Java on Windows 7 x64 machine from version 7 to 8. After that, the service which depends on Java failed to start ("net start SymmetricDS") because the system could not find Java executable.

I.e. services.exe (parent process of all LocalSystem services) does not pick up environment variables' changes itself. It just does so for new spawned child processes. I've confirmed it by looking at it with SysInternals Process Explorer. Proof: http://workblog.pilin.name/2013/03/service-environment-variables.html. It doesn't say which versions of Windows does it apply to, but the date of publication is rather new, so I think it applies to Windows 7 and Windows Server 2008.

Solved the problem by making symlinks to Java executables in one of folders in old PATH contained in services.exe.

  • i.e. in my case, services.exe could not start new service because it was located in a directory that was not in old PATH value cached by services.exe. – Alexandr Zarubkin Jul 07 '16 at 18:15
0

Possible via nasty hack while running. Otherwise only by restart.

Just verified that environment variables can be changed with Cygwin GDB. Tried with x86 Cygwin and x86 Winword. Verified with ProcExp.exe.

GDB commands found here: https://stackoverflow.com/questions/205064/is-there-a-way-to-change-another-processs-environment-variables

Microsoft says this isn't the regular way of doing this: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682009(v=vs.85).aspx

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.


My earlier edit:

My best guess: Service needs to be restarted. At least.

A process will upon creation inherit the enviroment variables from its parent. (http://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx)

So: The process must be restarted AND there must be something for it to inherit.

How to check
Check for yourself using ProcExp.exe. (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx ) Double-click the process you're interested in and then select the "Environment" tab.

To find the parent double-click the child, select the "Image" tab and read the "Parent:" row.

Repeat all the way up the heritage line.

StackzOfZtuff
  • 1,754
  • 12
  • 21
  • I understand how environment variable inheritance normally works. I'm asking about a specific situation here, one that was identified as an issue by Microsoft and later addressed in a hotfix for Win 2003. I've raised the bounty because I'm looking for a concrete, authoritative reference that addresses my specific question. I'm fully expecting a service restart is required, what's important is whether a **reboot** of the machine is needed. – Duncan Jones Nov 14 '14 at 09:33
  • So the question is: "Has the Win2003 KB887693 hotfix been integrated into out-of-the-box Windows Server 2008 or not?" Right? I can't give an authoritative source for that. (Maybe another helpful tidbit: [This KB951859](http://support.microsoft.com/kb/951859) mentions your KB887693 entry.) – StackzOfZtuff Nov 14 '14 at 10:16
  • Well, the question is simply "Do I need to reboot a Win2008 server so that a Local System service can pick up changes to environment variables". So far, the answers seem to have been based on the standard theories of processes and inheritance of env variables. I'm waiting for something a little more concrete, specifically in light of the presence of a Win2k3 hotfix. I will try to use your ProcExp ideas to probe this further myself with experiments. – Duncan Jones Nov 14 '14 at 10:19