Skip to content

Bash Scripts

Bash scripts run as standard shell scripts in a subprocess. Use them for system administration tasks, file operations, and chaining CLI tools.

Available tools

All standard Linux utilities available in the Debian Bookworm base image are accessible: curl, wget, jq, grep, awk, sed, sort, uniq, wc, tar, gzip, openssl, and more.

Injected variables

VariableDescription
AUTH_HEADERS_JSONJSON string of auth headers
TASK_PAYLOADWebhook or MQTT payload string (if triggered by one)

Parse auth headers with jq:

bash
# Extract a specific header value
TOKEN=$(echo "$AUTH_HEADERS_JSON" | jq -r '.Authorization')

Example: call a REST API with curl

bash
#!/bin/bash
set -euo pipefail

# Parse auth header
AUTH=$(echo "$AUTH_HEADERS_JSON" | jq -r '.Authorization // empty')

RESPONSE=$(curl -sf \
  -H "Authorization: $AUTH" \
  -H "Accept: application/json" \
  "https://api.example.com/status")

echo "Response: $RESPONSE"

STATUS=$(echo "$RESPONSE" | jq -r '.status')
echo "Service status: $STATUS"

if [ "$STATUS" != "ok" ]; then
  echo "ERROR: service is not healthy" >&2
  exit 1
fi

Example: health check with alerting

bash
#!/bin/bash

ENDPOINTS=(
  "https://service1.example.com/health"
  "https://service2.example.com/health"
  "https://service3.example.com/health"
)

FAILED=0

for URL in "${ENDPOINTS[@]}"; do
  STATUS=$(curl -sf -o /dev/null -w "%{http_code}" "$URL" || echo "000")
  if [ "$STATUS" = "200" ]; then
    echo "OK: $URL"
  else
    echo "FAIL: $URL (HTTP $STATUS)" >&2
    FAILED=$((FAILED + 1))
  fi
done

if [ "$FAILED" -gt 0 ]; then
  echo "$FAILED endpoint(s) unhealthy" >&2
  exit 1
fi

echo "All endpoints healthy"

Error handling

Use set -euo pipefail at the top of scripts to exit immediately on errors. A non-zero exit code marks the run as Failed.

Filesystem access

Bash scripts run as script-runner — a locked-down user with no write access to /app or /app/config. Temporary files can be written to /tmp and are cleaned up after execution.

ApiMeld Task Scheduler