Push notifications from bash
Your shell script finished. Your cron job ran — or didn’t. You find out by checking logs.
Here’s how to make bash scripts tell you directly.
The whole thing in one line
curl -s -X POST "https://trigger.fyi/$TRIGGER_FYI_SECRET_KEY" \
-H "Content-Type: text/plain" \
-d "Backup complete"Set TRIGGER_FYI_SECRET_KEY in your environment, run this after your work, done. Your phone notifies.
A reusable function
Put this in your .bashrc, .zshrc, or source it in scripts:
fyi() {
local title="$1"
local body="${2:-}"
local level="${3:-}"
local key="${TRIGGER_FYI_SECRET_KEY:-}"
[ -z "$key" ] && return 0
if [ -n "$body" ] || [ -n "$level" ]; then
local payload="{\"title\":\"$title\""
[ -n "$body" ] && payload="$payload,\"body\":\"$body\""
[ -n "$level" ] && payload="$payload,\"level\":\"$level\""
payload="$payload}"
curl -s -X POST "https://trigger.fyi/$key" \
-H "Content-Type: application/json" \
-d "$payload" &>/dev/null &
else
curl -s -X POST "https://trigger.fyi/$key" \
-H "Content-Type: text/plain" \
-d "$title" &>/dev/null &
fi
}
fyi_critical() { fyi "$1" "${2:-}" "critical"; }
fyi_log() { fyi "$1" "${2:-}" "log"; }Usage:
# Simple
fyi "Deploy complete"
# With detail
fyi "Deploy complete" "commit abc1234 · 42s"
# Urgent
fyi_critical "Deploy failed" "Exit code 1"
# Feed only, no push
fyi_log "Cache warmed"The &>/dev/null & sends the curl in the background — your script doesn’t wait for the notification.
In a cron job
#!/bin/bash
set -euo pipefail
START=$(date +%s)
# Your work here
rsync -az /data/ /backup/
END=$(date +%s)
ELAPSED=$((END - START))
fyi "Backup complete" "${ELAPSED}s"Cron doesn’t have environment variables by default. Set the key explicitly:
0 3 * * * TRIGGER_FYI_SECRET_KEY=your_key_here /usr/local/bin/backup.shOr export it in the script:
#!/bin/bash
export TRIGGER_FYI_SECRET_KEY="your_key_here"
# ... rest of scriptOn success or failure
#!/bin/bash
run_deploy() {
./deploy.sh
}
if run_deploy; then
fyi "Deploy succeeded" "$(git rev-parse --short HEAD)"
else
fyi_critical "Deploy failed" "$(git rev-parse --short HEAD)"
exit 1
fiOr with a trap:
#!/bin/bash
cleanup() {
if [ $? -ne 0 ]; then
fyi_critical "Script failed" "Line $LINENO"
fi
}
trap cleanup EXIT
# Your work here — any failure notifies via the trap
rsync -az /data/ /backup/
fyi "Sync complete"Timing
#!/bin/bash
START=$(date +%s)
do_work
ELAPSED=$(( $(date +%s) - START ))
MINUTES=$((ELAPSED / 60))
SECONDS=$((ELAPSED % 60))
fyi "Job complete" "${MINUTES}m ${SECONDS}s"In a Makefile
deploy:
./build.sh && ./deploy.sh
@curl -s -X POST "https://trigger.fyi/$(TRIGGER_FYI_SECRET_KEY)" \
-H "Content-Type: text/plain" \
-d "Deploy complete" || true
test:
npm test && \
curl -s -X POST "https://trigger.fyi/$(TRIGGER_FYI_SECRET_KEY)" \
-d "Tests passed" || trueThe || true prevents a failed notification from failing the make target.
Setup
npx trigger.fyiGenerates a key. Then add to your environment:
# ~/.bashrc or ~/.zshrc
export TRIGGER_FYI_SECRET_KEY=your_key_hereReload your shell, and fyi is available everywhere.
Related: Push notifications from Python · The curl guide to trigger.fyi · Cron job notifications · What is trigger.fyi?