-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathif_fail_do_notification
executable file
·34 lines (24 loc) · 1.03 KB
/
if_fail_do_notification
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env bash
# Send macOS notification if commans fails. Similar to ./if_fail_do_notification
# Requirements: terminal-notifier
# Limitation: can't be run in root cron job, as it's not connected to a GUI session: https://apple.stackexchange.com/a/257550/197493
cmd="$@"
scriptname=${0##*/}
file_stdout=$(mktemp -t ${scriptname}.XXXXXX)
file_stderr=$(mktemp -t ${scriptname}.XXXXXX)
exit_hook() {
echo "In exit_hook(), cleaning up files." >&2
rm $file_stdout $file_stderr
}
trap exit_hook INT TERM
# Con: can't separate $cmd stdout from stderr in calle of this script.
#eval "$cmd" 2>&1 | tee $file_stdout
# Con: stderr and stdout order is not preserved in the cat merge for notification.
# Reference: https://stackoverflow.com/a/692407/265508
eval "$cmd" > >(tee -a $file_stdout) 2> >(tee -a $file_stderr >&2)
ecode=$?
#ecode=${PIPESTATUS[0]}
#test $ecode -eq 0 || terminal-notifier -title "$cmd" < $file_stdout
test $ecode -eq 0 || cat $file_stderr $file_stdout | terminal-notifier -title "$cmd"
#rm $file_stdout $file_stderr
exit $ecode