Search code examples
azure-cliazure-batch

How to see if azure batch job exists from azure cli?


Currently I do the following to see if a job exists:

id=$(az batch job show \
    --job-id $1 \
    --query id \
    --output tsv \
    --account-key $AZ_BATCH_KEY \
    --account-name $AZ_BATCH_ACCOUNT \
    --account-endpoint $AZ_BATCH_ENDPOINT)

if [ "$id" == "" ]; then
    # create job
else
    # enable job
fi

Is there a more robust way to do this?

Basically, I want to check if a job already exists. If it does, I want to enable it and add tasks to it. Otherwise, I will create it and add tasks.

I am using the auto pool feature to create a pool on job creation and delete the pool on job termination.


Solution

  • Above looks fine to me, seems like another one possible way could be like this:

    Get list of the jobs and then you have a collection to parse, or filter.

    az batch job list [--account-endpoint]
                  [--account-key]
                  [--account-name]
                  [--expand]
                  [--filter]
                  [--job-schedule-id]
                  [--select]
    

    with regards to filter these links might be helpful:

    Doc link:

    With regards to robustness, I am not sure if there is any performance metrics around the specific commands. Might be worth to share the scenario for the performance optimization if that's at all in play here.