-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog-email.sh
More file actions
34 lines (29 loc) · 1.09 KB
/
log-email.sh
File metadata and controls
34 lines (29 loc) · 1.09 KB
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
#!/bin/sh
# A simple shell script/template to log progress and
# email errors before exiting
# FUTURE TODO: verify hostname is valid because why not?
# FUTURE TODO: verify mailx (or sendmail?) is installed
# FUTURE TODO: potentially switch from echo -e to printf
# since echo -e does funky things with formatting which
# could cause issues if piping is desired in the future
log_file="/var/log/example.log"
# Redirect stdout to log_file and THEN redirect stderr to log_file
exec 1>$log_file 2>&1
# Log info level
log_inf() {
echo "[INF] [$(date '+%m/%d/%Y %H:%M:%S')] $*"
}
# Log and email error level
# exit upon completion
log_err() {
echo "[ERR] [$(date '+%m/%d/%Y %H:%M:%S')] $*"
echo -e "The script $0 reports an error:\n\n[$(date '+%m/%d/%Y %H:%M:%S')] $*\n\n See \"$log_file\" on the machine for more details" | mail -s "ERROR: $0@$(hostname -s)" example-name@example.com
exit 1
}
# Example use
log_inf "Executing false if statement..."
if false; then
log_inf "If statement isn't true, so this will not be logged"
else
log_err "This statement will be logged and emailed"
fi