Run a Job from a shell script
Run one Completion on the server from a shell script. It submits a Job, waits until the Job ends, and saves the result to a file. You need curl 7.76 or later, jq, and the two shell variables of the Quickstart. Run a Job explains each route that the script uses.
Run the script
-
Download
job.sh, or save this script asjob.sh. Put your own Completion request in the body of the firstcurl.#!/bin/sh set -eu KEY=${1:-sky-$(date +%s)} job=$(curl -sS "$ACS_API_BASE/completion-jobs" \ -H "Authorization: Bearer $ACS_API_KEY" \ -H "Content-Type: application/json" \ -H "Idempotency-Key: $KEY" \ -d '{"model": "llama-8b", "prompt": "The sky is", "max_tokens": 2}') JOB_ID=$(echo "$job" | jq -r .id) echo "Submitted job $JOB_ID with Idempotency-Key $KEY" >&2 status=queued while [ "$status" = queued ] || [ "$status" = running ]; do sleep 1 status=$(curl -sS "$ACS_API_BASE/completion-jobs/$JOB_ID" \ -H "Authorization: Bearer $ACS_API_KEY" | jq -r .status) done echo "Job $JOB_ID is $status" >&2 curl -sS --fail-with-body "$ACS_API_BASE/completion-jobs/$JOB_ID/result" \ -H "Authorization: Bearer $ACS_API_KEY" -
Run the script with an
Idempotency-Keyfor this piece of work, and send its output to a file:sh job.sh sky-001 > result.jsonThe script writes its messages, such as
Job ... is succeeded, to standard error, so the file holds only the result. It exits with 22 when the result is an error, ascurl --fail-with-bodydoes, and the file then holds the error object. -
Read the result in
result.json. It is the Completion thatPOST /v1/completionswould have answered:result.json { "id": "cmpl-ab2a9dbbc18c302b", "object": "text_completion", "created": 1791781222, "model": "meta-llama/Llama-3.1-8B", "choices": [{ "index": 0, "text": " falling!", "logprobs": null, "finish_reason": "length", "prompt_logprobs": null }], "usage": { "prompt_tokens": 4, "completion_tokens": 2, "total_tokens": 6 } }
If the script stops
The Job runs in the API, not in the script, so it keeps running when the script stops. set -eu stops the script when a command fails, for example when curl cannot reach the API. Run the script again with the same Idempotency-Key. The API answers the Job that the key made. The script then saves its result, and the Completion does not run again.