Skip to content

Instantly share code, notes, and snippets.

@jasalt
Created January 10, 2026 09:52
Show Gist options
  • Select an option

  • Save jasalt/2bd15809a370e0c89a0e28bc59cdb500 to your computer and use it in GitHub Desktop.

Select an option

Save jasalt/2bd15809a370e0c89a0e28bc59cdb500 to your computer and use it in GitHub Desktop.
"Awaiting" D-Bus message in Bash
#!/usr/bin/env bash
# Senging expected message:
# dbus-send --session --type=signal /org/example/Test org.example.Test.TestSignal
set -euo pipefail
# Match the precise signal we’re interested in.
MATCH="type='signal',interface='org.example.Test',member='TestSignal'"
# Create a temporary FIFO to capture dbus-monitor output line-by-line.
FIFO=$(mktemp -u)
mkfifo "$FIFO"
cleanup() {
rm -f "$FIFO"
}
trap cleanup EXIT
# Start dbus-monitor with a timeout so we don’t block forever.
timeout 15s dbus-monitor --session "$MATCH" >"$FIFO" &
MON_PID=$!
# Consume the monitor output; react only when the actual signal arrives.
while IFS= read -r line; do
[[ $line == *"member=TestSignal"* ]] || continue
echo "Signal received"
kill "$MON_PID" >/dev/null 2>&1 || true
exit 0
done <"$FIFO"
# If we fall out of the loop, the monitor ended (timeout or other error).
wait "$MON_PID" >/dev/null 2>&1 || true
echo "Timed out without signal"
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment