Skip to content →

Windows Services Running as Local System and Environment Variables

As sometimes happens I installed an application on a Windows server and several days later another program stopped working.

I did my research and was able to determine that the installer for this new program had altered my PATH variable such that a specific DLL was now loading from the wrong place with an incompatible version causing the old program to stop working. Fix the path – everything works. All in a day’s work.

Except…

While I was able to successfully run the program from the command prompt the actual use was different. In fact this program was called from an Apache module that was shelling out to it. In this context the program was still failing. After checking to see if there was some Apache setting that was controlling the PATH variable and finding out that you can’t actually change environment variables in Apache on Windows I was able to confirm that the PATH value in the Apache context was different than the system PATH.

Now I knew that if you change the system PATH you need to close and reopen your command windows to reinitialize the environment variables so I assumed that restarting the Apache service would cause the same thing to happen. Not so much.

As it turns out my Apache service was running as Local System and evidently all the environment variables are set at startup time. That meant I needed to reboot the server. Unfortunately I can’t do that until our maintenance window several days from now and people need the function to work today.
Luckily there were several layers of code between Apache and the actual program that was failing. One layer was a C# application, so making use of the Environment.SetEnvironmentVariable("PATH", newPath, EnvironmentVariableTarget.Process) and then shelling out I was able to temporarily change the PATH to something that will work.

Now I just need to remember I did this 2 years from now when something else changes…

Published in DevOps Programming