-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.sh
80 lines (70 loc) · 2.08 KB
/
template.sh
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
#
# Description:
#
# [ shell functions at top. ]
# [ main program towards bottom of this file. ]
#
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/java/current/bin:/opt/jdk_current/bin
unalias -a 2>/dev/null
# GLOBAL VARIABLES
OS=`uname -s`
HOSTNAME=`hostname`
saveddir=`pwd`
PROGRAM_NAME=`basename $0`
LOG_FILE=/tmp/${PROGRAM_NAME}_${HOSTNAME}.log
ERROR_LOG=/tmp/${PROGRAM_NAME}_${HOSTNAME}.err
TMPDIR=/tmp
TEE_OPTS=' --append '
############################################################## FUNCTION CALLS
# email_log args should be: <ReturnValue = 0|1|2...> <Subject>
function email_log ()
{
local RETURNVALUE=$1
local EMAIL_SUBJECT=$2
local EMAIL_ADDR='[email protected]'
local HOSTNAME=`hostname`
local LOCAL_LOG_FILE=${LOG_FILE}.local
/bin/echo -e "${PROGRAM_NAME} Report.\n" > ${LOCAL_LOG_FILE}
/bin/echo -e "\n\nComplete log:\n" >> ${LOCAL_LOG_FILE}
cat ${LOG_FILE} >> ${LOCAL_LOG_FILE}
/bin/echo -e "\n\nError log:\n" >> ${LOCAL_LOG_FILE}
cat ${ERROR_LOG} >> ${LOCAL_LOG_FILE}
unix2dos ${LOCAL_LOG_FILE} >/dev/null 2>&1
## Ex.) To CC: a recipient, add "-c [email protected]" before EMAIL_ADDR
nohup cat ${LOCAL_LOG_FILE} | mail -v \
-s "FROM: ${HOSTNAME}: ${PROGRAM_NAME}: ${EMAIL_SUBJECT}" \
${EMAIL_ADDR} >> /tmp/${PROGRAM_NAME}_email.log 2>&1 &
exitprog $RETURNVALUE
}
function exitprog ()
{
cd $saveddir
exit $1
}
## END OF FUNCTIONS
#############################################################################
#
####################
## RUN MAIN PROGRAM
# user must have root access
if [[ "$UID" -ne "0" ]]
then
echo "User does NOT have root access!" 2>&1 | tee ${LOG_FILE} ${TEE_OPTS}
email_log 1 "ERROR: ${PROGRAM_NAME}: NO ROOT ACCESS!"
fi
# clear log files
cat /dev/null > ${LOG_FILE}
cat /dev/null > ${ERROR_LOG}
echo "Running ${PROGRAM_NAME} ... " 2>&1
# call function(s)
# finish program and email success
email_log 0 "Successfully executed program ${PROGRAM_NAME} ..."
####################
############## end of script
##
## CHANGES
## created ####-##-##
##
## AUTHOR:
##