|
| 1 | +#!/bin/bash |
| 2 | +################################################################################### |
| 3 | +# |
| 4 | +# Notify command completion and exit status via pushover and notify-send |
| 5 | +# |
| 6 | +# uses pushover.sh from https://raw.githubusercontent.com/jnwatts/pushover.sh/master/pushover.sh |
| 7 | +# |
| 8 | +# You need to either export PUSHOVER_API_KEY and PUSHOVER_USER_KEY, or set them in |
| 9 | +# ~/.pushover_keys.sh |
| 10 | +# |
| 11 | +################################################################################### |
| 12 | +# |
| 13 | +# Copyright 2015 Jason Antman <[email protected]> <http://www.jasonantman.com> |
| 14 | +# Free for any use provided that patches are submitted back to me. |
| 15 | +# |
| 16 | +# The most recent version of this script is available at: |
| 17 | +# <https://github.com/jantman/misc-scripts/blob/master/pushover> |
| 18 | +# |
| 19 | +################################################################################### |
| 20 | +# |
| 21 | +# EXAMPLES: |
| 22 | +# |
| 23 | +# pushover /bin/false |
| 24 | +# (sends failure notification) |
| 25 | +# |
| 26 | +# pushover /bin/true foo bar baz |
| 27 | +# (sends success notification) |
| 28 | +# |
| 29 | +################################################################################### |
| 30 | +# |
| 31 | +# Version 1.0.0 |
| 32 | +# |
| 33 | +# CHANGELOG: |
| 34 | +# |
| 35 | +# * 1.0.0 2015-05-11 Jason Antman <[email protected]> |
| 36 | +# * Initial public version |
| 37 | +# |
| 38 | +################################################################################### |
| 39 | + |
| 40 | + |
| 41 | +if ! which pushover.sh > /dev/null 2>&1; then |
| 42 | + >&2 echo "ERROR: pushover.sh not found; please put in path (download from https://raw.githubusercontent.com/jnwatts/pushover.sh/master/pushover.sh)" |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +[[ -f ~/.pushover_keys.sh ]] && source ~/.pushover_keys.sh |
| 47 | + |
| 48 | +if [ -z "$PUSHOVER_API_KEY" ]; then |
| 49 | + >&2 echo "ERROR: export PUSHOVER_API_KEY or set in ~/.pushover_keys.sh" |
| 50 | + exit 2 |
| 51 | +fi |
| 52 | + |
| 53 | +if [ -z "$PUSHOVER_USER_KEY" ]; then |
| 54 | + >&2 echo "ERROR: export PUSHOVER_USER_KEY or set in ~/.pushover_keys.sh" |
| 55 | + exit 3 |
| 56 | +fi |
| 57 | + |
| 58 | +stime=$(date '+%s') |
| 59 | +$@ |
| 60 | +exitcode=$? |
| 61 | +# timer |
| 62 | +etime=$(date '+%s') |
| 63 | +dt=$((etime - stime)) |
| 64 | +ds=$((dt % 60)) |
| 65 | +dm=$(((dt / 60) % 60)) |
| 66 | +dh=$((dt / 3600)) |
| 67 | +times=$(printf '%d:%02d:%02d' $dh $dm $ds) |
| 68 | +# end timer |
| 69 | +if [ "$exitcode" -eq 0 ] |
| 70 | +then |
| 71 | + pushover.sh -p 0 -t "Command Succeeded" -T "$APIKEY" -U "$USERKEY" "succeeded in ${times} on $(hostname): $@ (in $(pwd))" |
| 72 | + echo "(sent pushover success notification)" |
| 73 | + if which notify-send 2>&1 > /dev/null; then |
| 74 | + notify-send "Command Succeeded" "succeeded in ${times}: $@ (in $(pwd))" |
| 75 | + fi |
| 76 | +else |
| 77 | + pushover.sh -p 0 -s falling -t "Command Failed" -T "$APIKEY" -U "$USERKEY" "failed in ${times} (exit $exitcode) on $(hostname): $@ (in $(pwd))" |
| 78 | + echo "(sent pushover failure notification)" |
| 79 | + if which notify-send 2>&1 > /dev/null; then |
| 80 | + notify-send "Command Failed" "failed in ${times} (exit $exitcode): $@ (in $(pwd))" |
| 81 | + fi |
| 82 | +fi |
0 commit comments