Skip to content

Instantly share code, notes, and snippets.

@lifuzu
Created January 7, 2014 18:33
Show Gist options
  • Save lifuzu/8304186 to your computer and use it in GitHub Desktop.
Save lifuzu/8304186 to your computer and use it in GitHub Desktop.
Bash function collections
function buildlogcmd {
log=$1
shift
echo "Build: $*" >> "$log"
echo " `pwd`" >> "$log"
echo " `date`" >> "$log"
echo "" >> "$log"
$* 2>&1 | tee -a "$log"
status=${PIPESTATUS[0]}
if [ $status != 0 ]; then
echo "" >> "$log"
echo "FAILED" >> "$log"
echo ""
echo "FAILED"
exit $status
fi
echo "" >> "$log"
echo "OK" >> "$log"
}
# check the network connection
function check_connection() {
touch ignore.me
scp -o ConnectTimeout=10 ignore.me ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_FOLDER}
# if scp doesn't work, then send error email to let someone know.
if [ $? != 0 ]; then
sendemail -f ${FROM_ADDR} -t ${TO_USERS} -u "FATAL ERROR: Can't connect to ${REMOTE_USER}@${REMOTE_HOST} from ${host}" -m "Please have a check and run backup scripts again!!!" -s ${SMTP_SERVER}
exit 1
fi
}
# set trap to clean environment
function clean_env() {
rm -f ${TARFILE}
rm -f ${TARFILE}.md5sum
rm -f ${LOGFILE}
}
trap clean_env EXIT SIGINT SIGTERM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment