You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Azure CLI检测Azure Batch作业是否存在?寻求更稳健方案

Great question! Your current approach works for basic checks, but we can make it more robust by leaning on exit codes (instead of parsing output, which can be unreliable) and adding better error handling for edge cases like network blips or auth issues. Here's an improved implementation that aligns perfectly with your core needs:

#!/bin/bash
set -eo pipefail  # Exit immediately on error, and propagate errors in pipelines

JOB_ID="$1"

# Check if job exists by capturing the exit code of `az batch job show`
if az batch job show \
    --job-id "$JOB_ID" \
    --account-key "$AZ_BATCH_KEY" \
    --account-name "$AZ_BATCH_ACCOUNT" \
    --account-endpoint "$AZ_BATCH_ENDPOINT" \
    >/dev/null 2>&1; then
    # Job exists: Check its state and enable if needed, then add tasks
    echo "Job $JOB_ID exists. Checking state..."
    JOB_STATE=$(az batch job show \
        --job-id "$JOB_ID" \
        --query state \
        --output tsv \
        --account-key "$AZ_BATCH_KEY" \
        --account-name "$AZ_BATCH_ACCOUNT" \
        --account-endpoint "$AZ_BATCH_ENDPOINT")
    
    if [[ "$JOB_STATE" == "disabled" ]]; then
        echo "Enabling job $JOB_ID..."
        az batch job enable \
            --job-id "$JOB_ID" \
            --account-key "$AZ_BATCH_KEY" \
            --account-name "$AZ_BATCH_ACCOUNT" \
            --account-endpoint "$AZ_BATCH_ENDPOINT"
    fi

    echo "Adding tasks to job $JOB_ID..."
    # Replace with your actual task addition command(s)
    az batch task add \
        --job-id "$JOB_ID" \
        --task-id "task-$(date +%s)" \
        --command-line "/bin/bash -c 'echo Hello World'" \
        --account-key "$AZ_BATCH_KEY" \
        --account-name "$AZ_BATCH_ACCOUNT" \
        --account-endpoint "$AZ_BATCH_ENDPOINT"

else
    # Job does not exist: Create it with auto-pool, then add tasks
    echo "Job $JOB_ID does not exist. Creating job with auto-pool..."
    az batch job create \
        --job-id "$JOB_ID" \
        --pool-auto-specification "{ \"autoPoolIdPrefix\": \"pool-$JOB_ID\", \"pool\": { \"vmSize\": \"Standard_D2s_v3\", \"osFamily\": \"2022-datacenter-core\" }, \"keepAlive\": false, \"poolLifetimeOption\": \"job\" }" \
        --account-key "$AZ_BATCH_KEY" \
        --account-name "$AZ_BATCH_ACCOUNT" \
        --account-endpoint "$AZ_BATCH_ENDPOINT"

    echo "Adding tasks to new job $JOB_ID..."
    # Replace with your actual task addition command(s)
    az batch task add \
        --job-id "$JOB_ID" \
        --task-id "task-$(date +%s)" \
        --command-line "/bin/bash -c 'echo Hello World'" \
        --account-key "$AZ_BATCH_KEY" \
        --account-name "$AZ_BATCH_ACCOUNT" \
        --account-endpoint "$AZ_BATCH_ENDPOINT"
fi

Key Improvements:

  • Exit Code Reliance: Instead of checking if the id output is empty, we use the exit code of az batch job show. This command returns 0 only if the job exists and the request succeeds—this avoids false positives from unexpected CLI output.
  • Error Handling: set -eo pipefail ensures the script exits immediately if any command fails, preventing unintended actions (like creating a job when the existence check failed due to a network error).
  • State Check: Before enabling the job, we verify its current state. This avoids unnecessary az batch job enable calls if the job is already active/completed.
  • Clean Output Redirection: We redirect stdout and stderr to /dev/null during the existence check to keep the script output clean.
  • Auto-Pool Configuration: Included a sample auto-pool specification in the job create command (adjust the VM size, OS family, and other settings to match your needs).

Additional Tips:

  • Task Batching: If you have multiple tasks to add, use az batch task create with a JSON file (via --json-file) instead of individual add commands for better performance.
  • Idempotency: Ensure your task IDs are unique or use --allow-task-reuse if you want to avoid duplicate tasks.
  • Logging: Add logging to a file (e.g., >> batch_job_logs.txt 2>&1) to track actions for debugging.

内容的提问来源于stack exchange,提问作者Dan McGrath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:10:54