Steve
Steve

Reputation: 4695

Set Environment Variable for Windows Service

I have a batch job that kicks off a windows service

sc serverName start serviceName

I'd like to set an environment variable for that service when I kick it off, similar to how you would do it for a subprocess. How can I do that with a windows service?

To clarify, I don't want to set any global environment variables, just one for that particular process. And I don't have access to the actual Service code.

Upvotes: 16

Views: 8752

Answers (2)

Aaron Jensen
Aaron Jensen

Reputation: 26769

Adapted from this answer on Server Fault:

Add a multi-string "Environment" value to the service's registry entry to the key at HKLM\SYSTEM\CurrentControlSet\Services\SERVICE_NAME. Each string value should be a name=value pair, where the name is the environment variable name you want the service to have, and the value is, well, the environment variable's value.

For example, if you want your service to use a different TEMP and TMP environment variables, you would enter TEMP=VALUE as one string and TMP=VALUE as another string.

Restart the service for it to get the new environment variables.

Upvotes: 5

VWeber
VWeber

Reputation: 1261

I'dont see wich language you use, but assuming you're using C# and your class derived from ServiceBase you have the method

protected override void OnStart(string[] args)

that contains your params in the string array. So starting your service with

sc serverName start serviceName param1 param2

will do the job.

Upvotes: 6

Related Questions