Shell Scripts Integration
Create reusable notification functions for your bash scripts.
Reusable Function
Terminal
#!/bin/bash
PING_URL="${PING_WEBHOOK_URL:-YOUR_WEBHOOK_URL}"
notify() {
local title="$1"
local body="$2"
local priority="${3:-normal}"
curl -s -X POST "$PING_URL" \
-H 'Content-Type: application/json' \
-d "{\"title\": \"$title\", \"body\": \"$body\", \"priority\": \"$priority\"}"
}
# Usage examples
notify "Backup Complete" "Database backed up successfully"
notify "Alert" "Disk space low on /dev/sda1" "high"
# In a script
if ./process_data.sh; then
notify "Process Complete" "Data processed successfully"
else
notify "Process Failed" "Check logs for details" "high"
fiSource from Separate File
Create notify.sh:
Terminal
#!/bin/bash
PING_URL="${PING_WEBHOOK_URL:-}"
ping_notify() {
if [ -z "$PING_URL" ]; then
echo "Warning: PING_WEBHOOK_URL not set" >&2
return 1
fi
local title="$1"
local body="${2:-}"
local priority="${3:-normal}"
local payload="{\"title\": \"$title\", \"priority\": \"$priority\""
if [ -n "$body" ]; then
payload="${payload}, \"body\": \"$body\""
fi
payload="${payload}}"
curl -s -X POST "$PING_URL" \
-H 'Content-Type: application/json' \
-d "$payload" > /dev/null
}Source it in other scripts:
Terminal
#!/bin/bash
source /path/to/notify.sh
# Now you can use ping_notify anywhere
ping_notify "Script started"
# ... do work ...
ping_notify "Script complete" "Processed 100 files"Error Trap
Automatically notify on script errors:
Terminal
#!/bin/bash
PING_URL="${PING_WEBHOOK_URL}"
trap 'notify_error' ERR
notify_error() {
local line_no=$LINENO
local command="$BASH_COMMAND"
curl -s -X POST "$PING_URL" \
-H 'Content-Type: application/json' \
-d "{\"title\": \"Script Error\", \"body\": \"Line $line_no: $command\", \"priority\": \"high\"}"
}
# Your script continues...
# Any error will trigger a notificationScript Wrapper
Wrap any command with notifications:
Terminal
#!/bin/bash
PING_URL="${PING_WEBHOOK_URL}"
CMD="$*"
echo "Running: $CMD"
if eval "$CMD"; then
curl -s -X POST "$PING_URL" \
-H 'Content-Type: application/json' \
-d "{\"title\": \"✅ Command Complete\", \"body\": \"$CMD\"}"
else
EXIT_CODE=$?
curl -s -X POST "$PING_URL" \
-H 'Content-Type: application/json' \
-d "{\"title\": \"❌ Command Failed\", \"body\": \"$CMD (exit $EXIT_CODE)\", \"priority\": \"high\"}"
exit $EXIT_CODE
fiUsage:
Terminal
./ping-wrap.sh npm run build
./ping-wrap.sh python train_model.py