pythonazureazure-batch

Get Azure Batch job and task ID from python


When running a custom python script using Azure Batch I am interested in getting access to, and logging, the JobID and TaskID of the tasks I am running on the compute node. Mainly for later traceability and debugging purposes. My desire is to get access to these parameters from within the python script that is being executed on the compute node. I have trawled through the Microsoft documentation, but have been unable to figure out how to access this information.

I had assumed that it was similar to the way I'm accessing the custom job parameters through the activity.json, but that does not appear to be the case.

How do I get the job ID and task ID of the Azure Batch task from within the python script that is being executed by the task?


Solution

  • Azure Batch populates various runtime environment variables for use while tasks are executed on compute nodes. You can retrieve the job and task id via the environment variables AZ_BATCH_JOB_ID and AZ_BATCH_TASK_ID, respectively. See the complete list of environment variables.