# Get step status in pre-exit hook

**URL:** https://forum.buildkite.community/t/get-step-status-in-pre-exit-hook/4451
**Category:** General
**Created:** [August 6, 2025, 1:18am UTC](https://forum.buildkite.community/t/get-step-status-in-pre-exit-hook/4451 "2025-08-06T01:18:52Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ianwremmel](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@ianwremmel](https://forum.buildkite.community/u/ianwremmel)
#### Post date: [August 6, 2025, 1:18am UTC](https://forum.buildkite.community/t/get-step-status-in-pre-exit-hook/4451/1 "2025-08-06T01:18:52Z")

</div>

I use the `pre-exit` script below to produce annotations linking to external URLs logged in each step. At this point, I have a lot of steps, so the annotations are getting pretty noisy. I’d like to detect the job’s status, and use `info` or `error` accordingly to make it easier to spot the annotations of interest.

I saw that [I can use buildkite-agent to get a step’s state](https://buildkite.com/docs/agent/v3/cli-step#getting-a-step-example), but it appears the state is always `running` in the job’s `pre-exit` hook (though I should admit I haven’t tried to simulate a failure yet). Is there any way to do what I’m trying to do?

```auto
#!/usr/bin/env bash

set -euo pipefail

lines=$(awk '/cloud.nx.app/' "$BUILDKITE_JOB_LOG_TMPFILE")
if [-z "$lines"]; then
  exit 0
fi

urls=$(echo "$lines" | grep -o 'https[^]\+' | sort | uniq)
if [-z "$urls"]; then
  exit 0
fi

cat << EOT | buildkite-agent annotate --style info --context "nx-$BUILDKITE_JOB_ID"
## $BUILDKITE_LABEL NX Cloud Runs

$(echo "$urls" | awk '{print "- ["$1"]("$1")"}')
EOT

```

---

<div class="post-metadata">

### Author: ![lizette](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/lizette/32/1161_2.png) [@lizette](https://forum.buildkite.community/u/lizette)
#### Post date: [August 6, 2025, 1:30am UTC](https://forum.buildkite.community/t/get-step-status-in-pre-exit-hook/4451/2 "2025-08-06T01:30:03Z")

</div>

Hi @ianwremmel ,

You can use the `BUILDKITE_COMMAND_EXIT_STATUS` environment variable at the `pre-exit` hook to check if your command step has failed or not. You can check about it [here](https://buildkite.com/docs/pipelines/configure/environment-variables#BUILDKITE_COMMAND_EXIT_STATUS) .

Cheers!

---

<div class="post-metadata">

### Author: ![ianwremmel](https://avatars.discourse-cdn.com/v4/letter/i/839c29/32.png) [@ianwremmel](https://forum.buildkite.community/u/ianwremmel)
#### Post date: [August 6, 2025, 1:41am UTC](https://forum.buildkite.community/t/get-step-status-in-pre-exit-hook/4451/3 "2025-08-06T01:41:45Z")

</div>

oh, sweet! I spent way too long looking through those variables and still didn’t see it 🙂 . thanks!
