-3

我正在寻找通用 bash(也可能是 python)包装器,它可以用来代替原始程序被调用,并且将调用原始程序并且:

  • 记录调用命令行
  • 记录标准输出和标准错误,但不对调用者隐藏它们
  • 透明 - 与被调用程序具有相同的标准输出
  • 返回被调用程序的退出代码

答案将是一个功能脚本,这里是“原型”

#!/bin/bash
LOGFILE=history.log
ORIGINAL=ls
touch ${LOGFILE}
echo "started" >>"${LOGFILE}"
ls "$@"
echo "ended with $?" >>"${LOGFILE}”
4

1 回答 1

0

也在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
于 2012-04-18T06:20:42.413 回答