#! /bin/ksh # ############################################################################################ # example.ksh # # Copyright: (C) 2002, Peter J. Dominey # # http://www.dominey.biz # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ###################################################################### # # Script to use as an example of how to use and manage log and error files # This script should be copied and pasted in to autosys job scripts. # The comments can be removed when placed in to a working script. The ERRFILE and LOGFILE # variables should be used for the AutoSys job definiton E.G. # std_out_file: /tmp/${AUTO_JOB_NAME}_$$.log # std_err_file: /tmp/${AUTO_JOB_NAME}_$$.err # or if a unique file name is not required (see note below) # std_out_file: /tmp/${AUTO_JOB_NAME}_0.log # std_err_file: /tmp/${AUTO_JOB_NAME}_0.err # Substatute /tmp for the path to wherever the log files should be place. # ############################################################################################ # # J P Morgan Chase # Peter Dominey 469-477-2035 2/4/03 # ############################################################################################ # # Modifications # By On Details # # ############################################################################################ # Define variables, note explinations: HOST=`hostname` TMPDIR=/tmp ############################################################################################ # Change the LGODIR variable to whereever log files are to be placed LOGDIR=/tmp ############################################################################################ # If a UNIQUE ID for log files is not required (ie there are to be overwritten each time) # Set UNIQUE to 0 #UNIQUE=0 UNIQUE=$$ ############################################################################################ # Provided simply to have standard temp working files TMPFILE1=${TMPDIR}/`basename $0`.tmp1 TMPFILE2=${TMPDIR}/`basename $0`.tmp2 ############################################################################################ # Use the AutoSys varialbe AUTO_JOB_NAME as the log file name, or if not available # E.G when the script is run from the command line, use the basename of the shell # script to define the log files. ERRFILE=${LOGDIR}/${AUTO_JOB_NAME:=`basename $0`}_${UNIQUE}.err LOGFILE=${LOGDIR}/${AUTO_JOB_NAME:=`basename $0`}_${UNIQUE}.log ############################################################################################ # If rquired a host to send the log files back to if the shell script fails and the # user id and password under which to ftp the files back LOGHOST=tmrutdc1 U= P= ############################################################################################### # Just to ensure the log files are emptied, comment out if files are to be appended to #Inialize files cat /dev/null >${LOGFILE} cat /dev/null >${ERRFILE} ################################################################################################ #Define any functions ############################################################################################ #Define a function to send files back at any point # Send the log file and error file back to a machine sendlogfiles () { log_msg "Transmitting log files " gc=`ftp -vn >>${LOGFILE} 2>>${ERRFILE} <>${ERRFILE} # Uncomment to send log files back to centeral machine E.G the tivoli server # sendlogfiles exit ${1} fi } ############################################################################################ # Define function to log whatever messages are wanted # Log file written to in a standard format. Although lengthy it conforms to the standard # UNIX syslog format and therefore can be used to be monitored by tools like Tivoli's log # file adaptor, logwatch, logcolorise etc. log_msg() { # Parameter 1 is text to write to the log echo "`date +'%b %d %H:%M:%S '`${HOST} $0[$$]: ${1}" >>${LOGFILE} } ############################################################################################ ############################################################################################ #Example use ## Execute a command: ls -lart ## Check the return command from the last command check_errs $? "Failed at ls command" # If the return code was good (0) log a message and continue log_msg "ls command Done" ## execute another command and so on # Note std_out in AutoSys job deffintion would ensure output # went to the std_out_file, but can also be spcified at the command E.G df -k >>${LOGFILE} check_errs $? "Failed at df command" log_msg "df command Done" # and is you want to see how is handles a failure: dffffgghhhhjkkkkllo check_errs $? "Failed at ddffffgghhhhjkkkkll command" 257 # Won't reach here as failure handle in line above log_msg "dffffgghhhhjkkkkllo command Done" ############################################################################################ ############################################################################################