如何通过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
idoutput is empty, we use the exit code ofaz batch job show. This command returns0only if the job exists and the request succeeds—this avoids false positives from unexpected CLI output. - Error Handling:
set -eo pipefailensures 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 enablecalls if the job is already active/completed. - Clean Output Redirection: We redirect
stdoutandstderrto/dev/nullduring the existence check to keep the script output clean. - Auto-Pool Configuration: Included a sample auto-pool specification in the
job createcommand (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 createwith a JSON file (via--json-file) instead of individualaddcommands for better performance. - Idempotency: Ensure your task IDs are unique or use
--allow-task-reuseif 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
相关产品推荐
相关产品推荐

