#!/usr/bin/ksh # INSTALLATION: # Copy this script as /opt/bin/tee.sh and give execute rights # # See article at # http://www.sun.com/bigadmin/content/submitted/save_with_tee.jsp # # CONFIGURATION: # Script is started with the interpreter specified in $TEE_SHELL # variable. If variable is not set, the default interpreter is started # according to the script file's name extension: # - *.sh started with /usr/bin/ksh # - *.pl started with /usr/bin/perl # - *.rb started with /usr/bin/ruby # - *.py started with /usr/bin/python # output is logged into /var/tmp directory, export the TEE_LOGDIR to change # the directory # # USAGE: # There are 2 different ways: # 1. starting directly from command line # /opt/bin/tee.sh # 2. put into a script as the first line # #!/usr/bin/ksh /opt/bin/tee.sh # tee.sh starts automatically by calling the script and # executes the original script by saving the output with tee # # Both ways have the same functionality: # - the corresponding interpreter is started according to the file # extension or the $TEE_SHELL variable # - output is logged into $TEE_LOGDIR or if it is not specified # into the /var/tmp # - log file is the command or script name postfixed with date # e.g: myscript_2007_03_23_11_11_22.log # # Bug (not really a big one): # STDERR is redirected to STDOUT, not possible to separate them any more # # Example: # uncomment, save following lines and execute to see how it works # #!/usr/bin/ksh /opt/bin/tee.sh # # default_cycles=3 # default_sleeping=1 # cycles=${1:-$default_cycles} # sleeping=${2:-$default_sleeping} # i=0 # while [[ $i -lt $cycles ]] # do # echo "$(date): $i" # sleep $sleeping # i=$((i+1)) # done # configuration setting, default values : ${TEE_LOGDIR:=/var/tmp} filename=`basename $1` if [[ -z $TEE_SHELL ]] ;then # starting the interpreter according to its extension, .pl perl, .rb ruby ... extension=${filename##*.} case $extension in pl) TEE_SHELL=/usr/bin/perl ;; rb) TEE_SHELL=/usr/bin/ruby ;; py) TEE_SHELL=/usr/bin/python ;; *|sh) TEE_SHELL=/usr/bin/ksh ;; esac fi filename=${filename%.*} LOGFILE=$TEE_LOGDIR/${filename}_`date +%Y_%m_%d_%H_%M_%S`".log" # echo "start command : $TEE_SHELL $@" exec $TEE_SHELL $@ 2>&1 | tee -a $LOGFILE ############################################################################## ### This script is submitted to BigAdmin by a user of the BigAdmin community. ### Sun Microsystems, Inc. is not responsible for the ### contents or the code enclosed. ### ### ### Copyright Sun Microsystems, Inc. ALL RIGHTS RESERVED ### Use of this software is authorized pursuant to the ### terms of the license found at ### http://www.sun.com/bigadmin/common/berkeley_license.jsp ##############################################################################