0

I am trying to use the Batch service-defined environment variables, but I keep getting a null error. Similar to what I saw in the samples, I am trying to get the jobID for a job that I created through Azure portal as follows:

private readonly string jobID = Environment.GetEnvironmentVariable("AZ_BATCH_JOB_ID");

But jobID returns null. Why so? What am I missing here?

Thanks!

J.B
  • 137
  • 2
  • 16
  • Is this run for the start task or a regular task? – fpark Jul 11 '17 at 14:09
  • @fpark this run is not for the start task, it's actually for the job manager task – J.B Jul 11 '17 at 16:51
  • What is your task commandline (removing sensitive portions)? You could try something like: `cmd.exe /c "set"` to see if you get `AZ_BATCH_JOB_ID` in the output. – fpark Jul 11 '17 at 18:13
  • @fpark I don't get any of the batch-service environment variables in the output – J.B Jul 11 '17 at 18:42
  • I'm not sure why that would be happening. Are you positive that you are running this as a task and not ad-hoc remotely logged in to the compute node? – fpark Jul 12 '17 at 15:16
  • @fpark yeah I think that's the problem, I was connected remotely to the compute node and was trying to run the task there. Thanks! – J.B Jul 12 '17 at 15:42
  • Cool, I'll move my comment as the answer. – fpark Jul 12 '17 at 15:43

1 Answers1

2

Azure Batch environment variables set on the compute node are only set when the task executes and is not set for remotely logged in users (since there is no job/task context for a remotely logged in user).

fpark
  • 2,304
  • 2
  • 14
  • 21
  • Is there a way, we can access environment variables across all the users of a machine? Currently, I am running a windows service on a batch node, and it listens to events from a service bus and perform actions accordingly. Those actions try to retrieve the app package directory using an environment variable, – Mike143 Jan 03 '20 at 23:17
  • You will need to persist the environment variables in the context of the window service via the start task. I.e., bind an application package with your start task then use windows functionality (e.g., env var to the registry) to persist that environment variable for the appropriate context. – fpark Jan 06 '20 at 16:46
  • Thanks, I should have read the documentation before posting this question – Mike143 Jan 11 '20 at 06:54