也在https://gist.github.com/2411440
#!/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