Skip to content

Instantly share code, notes, and snippets.

@ssbarnea
Created April 18, 2012 06:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssbarnea/2411440 to your computer and use it in GitHub Desktop.
Save ssbarnea/2411440 to your computer and use it in GitHub Desktop.
Bash wrapper script that calls another exec and logs it’s execution output and return code
#!/bin/bash
# install:
# * rename the original script to script.original
# * rename the wrapper.sh to script
# * DONE! all calls are going to be logged now
TIMESTAMP=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
ME=`basename $0`
WRAPPED_COMMAND=$ME.original
echo "${TIMESTAMP} exec: $0 $@" >>${ME}.stdout.log
#${WRAPPED_COMMAND} "$@"|tee -a "${LOGFILE}"
${WRAPPED_COMMAND} "$@" > >(tee -a ${ME}.stdout.log) 2> >(tee -a ${ME}.stderr.tmp >&2)
RET=$?
TIMESTAMP_END=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
if [ -s ${ME}.stderr.tmp ] # if we have something logged to stderr (not usual)
then
echo "${TIMESTAMP} exec: $0 $@" >>${ME}.stderr.log
cat >> ${ME}.stderr.log
echo "${TIMESTAMP_END} retured: ${RET}" >>${ME}.stderr.log
fi
rm -f "${ME}.stderr.tmp"
echo "${TIMESTAMP_END} retured: ${RET}" >>${ME}.stdout.log
exit $RET
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment