boltZharp Docs
terminalSDKs

Shell scripts

zharp.sh reports the command that made a script fail — its exit code, line, the function stack and the code around it — and runs any command as one span with zharp run. For cron jobs, deploy and backup scripts and CI steps.

info
zharp.sh 0.1.0

This SDK is new, and its API may still change before 1.0. Requires bash or zsh for the hook (any POSIX sh for zharp run), and curl or wget.

One file, two ways in: source it at the top of a bash or zsh script, or put zharp run -- in front of any command. No binary to install, nothing to compile. It sends with curl, and a dead network never changes the script's result.

Install

sudo curl -fsSL https://zharp.io/zharp.sh -o /usr/local/lib/zharp.sh
sudo chmod +x /usr/local/lib/zharp.sh
sudo ln -sf /usr/local/lib/zharp.sh /usr/local/bin/zharp   # the zharp command
# check it against https://zharp.io/zharp.sh.sha256

Set ZHARP_KEY, from the app's settings in Zharp, where the script runs: its environment, the crontab or a systemd unit's Environment=. The app's setup page in Zharp shows these steps with the key filled in.

The hook: bash and zsh scripts

#!/usr/bin/env bash
source /usr/local/lib/zharp.sh       # ZHARP_KEY from the environment

rsync -a /data backup:/data

The script stops on its first failing command (set -Eeo pipefail; in zsh ERR_EXIT and PIPE_FAIL) — without it most failures scroll past unnoticed. That command is reported with its exit code, the line, the function stack and the code around it, and the last 50 lines of stderr. The run is one span: the script's name, duration and exit code. ZHARP_ERREXIT=false leaves the script's own error behaviour; then a non-zero exit is reported with its code.

  • A failing command in a pipeline is named, not the pipeline's last command.
  • exit 3 is reported as the script exiting with 3, with the end of stderr.
  • Plain sh (dash, busybox) has no ERR trap: sourcing the file there reports only the exit code. Use zharp run instead.
  • An interactive shell that sources it gets the functions only: no traps, nothing stops.

zharp run: any command

zharp run --service nightly-backup -- /opt/jobs/backup.sh
zharp run -- python3 import.py                 # any language, any binary
zharp run --name "db dump" -- pg_dump shop > shop.sql

The command runs as one span: its duration, its exit code, and on failure the end of its output. Its output goes where it went and the exit code is passed through unchanged. A script under zharp run that also sources the hook reports its own error once, in the same trace. Plain POSIX sh is enough.

Cron job monitors

A Cron job monitor alerts when a job fails or never runs. --monitor takes the token from the monitor's ping URL: it is pinged at start and with the exit code (and the end of the output on failure), with the run's trace, so the monitor's run links to the error.

ZHARP_KEY=zpk_…
0 3 * * *  zharp run --service nightly-backup --monitor YOUR_MONITOR_TOKEN -- /opt/jobs/backup.sh

With the hook, set ZHARP_MONITOR before sourcing:

export ZHARP_MONITOR=YOUR_MONITOR_TOKEN
source /usr/local/lib/zharp.sh

Logs

Off by default. With ZHARP_LOGS=true, stdout and stderr are sent line by line with the trace (stderr lines say so), and still printed as before.

ZHARP_LOGS=true ./backup.sh
ZHARP_LOGS=true zharp run -- ./backup.sh

zharp_log prints a line ending in trace_id=… span_id=…, so the Zharp agent reading the script's log links it to the run, and sends it.

zharp_log "copied 120 files"            # prints: copied 120 files trace_id=… span_id=…  (and sends it)
zharp_log -l warn "disk 91% full"

Logs only

If you only want logs, turn tracing off. Logs, errors, and the user and session they belong to are still sent; no spans are recorded and no trace headers are added to outgoing requests. A request from a Zharp front end is still read, so its logs keep the caller's user, session and trace ID.

ZHARP_KEY=zpk_… ZHARP_LOGS=true zharp run --service nightly-backup -- ./backup.sh

The logs show on the Logs page under the service name, and on the app's Logs tab with the user and session of each line. The key comes from an app — see Get a key. To add tracing later, remove the option; nothing else changes.

Errors

An error is a CommandFailed, with the command, its exit code and the stack written the way PowerShell writes its own, so both read the same in Zharp:

CommandFailed: rsync exited with 23: rsync -a /data backup:/data
at copy_files, /opt/jobs/backup.sh: line 42
at main, /opt/jobs/backup.sh: line 60

With it go the code around each of the innermost five frames, read from the script file, and the end of stderr. Issues are grouped by the functions and file, not the line numbers or exit code. zharp.sh's own lines never show.

Readable stack traces

Nothing to upload: the stacks this SDK sends already name your functions, files and lines, and issues are grouped by them.

Tracing

TRACEPARENT is exported, so programs the script starts with a Zharp SDK continue the trace; a script started inside a trace continues it in turn. zharp_curl adds the trace to a curl call, as a span of its own.

source /usr/local/lib/zharp.sh

node sync.js                                   # continues the trace (TRACEPARENT is exported)
zharp_curl -fsS -X POST https://api.example.com/sync   # a span, and a traceparent header

Options

Environmentzharp runDefault
ZHARP_KEY—— nothing is sent without one
ZHARP_SERVICE, OTEL_SERVICE_NAME--servicethe script's or command's name
ZHARP_ENVIRONMENT——
ZHARP_RELEASE——
ZHARP_ENDPOINT—https://api.zharp.io/api/v1/otlp
ZHARP_MONITOR--monitor— a Cron job monitor's token or ping URL
—--namethe span's name: the command's
ZHARP_LOGS—false. See Logs.
ZHARP_ERREXIT—true: stop on the first error (the hook)
ZHARP_CAPTURE_OUTPUT—true: keep the end of stderr for the error (the hook)
ZHARP_DISABLED=true—turns everything off
ZHARP_DEBUG=true—says on stderr when something can't be sent

Your own EXIT trap

The hook sends at exit from an EXIT trap. A script that sets its own replaces it; call zharp_finish from yours:

source /usr/local/lib/zharp.sh
trap 'cleanup; zharp_finish $?' EXIT     # your own EXIT trap replaces Zharp's: call zharp_finish in it

Supported versions

  • The hook: bash 4 and 5, zsh 5. bash 3.2 (macOS's) works, but names the line where a function starts rather than the failing line inside it.
  • zharp run: any POSIX sh — bash, dash, busybox — with curl or wget. Linux and macOS.

Troubleshooting

Nothing arrives

On the app's setup page (Applications → the app → settings → Setup instructions), the verify step shows the first event as soon as it lands, and Send a test event checks the key on its own. If the test event arrives but your app's don't, the SDK isn't starting or isn't reaching Zharp:

  • Is init called, and before the code you expect to see? Without a key, the SDK starts nothing and says so once.
  • Does the service name match? With an app key everything goes to that app whatever it says; with a workspace key, service decides the app — see Keys.
  • Data is sent in batches every few seconds. A process that exits straight away should call flush first.
  • Logs and errors arrive but no traces? Check that tracing isn't turned off — see Logs only.
  • Is curl (or wget) installed? Without either, zharp.sh says so once and sends nothing.
  • Does the script set its own EXIT trap after sourcing? Then call zharp_finish in it.
  • A cron job runs with almost no environment: put ZHARP_KEY=… at the top of the crontab.

401 or 403

Ingest answers 401 when the key is missing, mistyped, revoked or rolled, and 403 when the request comes from an origin or IP address the key doesn't allow. Neither is retried: the SDK warns once and drops what was refused, and it never throws into your code. Copy the key again from the app's settings.