Capturing Console Output

Background

I was looking through a Bash Script and I was particularly focused on how logging is being accomplished.

If not done fully well, each step will have two steps.  One will write to the screen ( stdout) and the other to a log file.

tee

I am familiar with tee, but my awareness came about through MS Windows.

It basically allows one to capture stdout output to a file, as well.

Linux /Bash

Outline

In our exercise, we will have two applications.

One a client and the other a controller.

The client will do the work and the controller will co-ordinate and ensure that the client’s work is captured.

Steps

  • Client
    • Do very little
      • Simple commands such as
        • hostname
        • date
        • uptime
  • Controller
    • Also do so little
      • Generate Log file names
        • Base file name
        • Timestamp file name
      • Invoke client and capture into base file name
      • Copy base file unto TS file

 

App – Client


#!/bin/bash

echo -n  "Host :-  "

uname -n

echo "Date :- $(date)"

echo "uptime"

uptime

App – Manager


#!/bin/bash

function getLogFileName()
{

  local _baseFolder=${1}
  local _addTS=${2}
  local _filename=""
  local _moduleName=""
  local _TSFormat="%Y%m%d_%H%M%S"
  local _TSAsString=""

  _moduleName=`basename "$0"`

  _TSAsString=$(date +$_TSFormat)

  _filename="${_baseFolder}/${_moduleName}"

  if [ $_addTS -eq 1 ]
  then

    _filename="${_filename}_${_TSAsString}"

  fi

  _filename="${_filename}.log"

  echo $_filename
}

#Get Log File Name ( base )
_filenameLogBase=$(getLogFileName '/tmp' '0' )

#Get Log File Name ( TS )
_filenameLogTS=$(getLogFileName '/tmp' '1' )

#As part of debug display Log file name
echo "Log file name is $_filenameLogTS"

# indicate client app
client="/tmp/stdout.capture.client.sh"

#execute app and log output to _filename
(exec "$client") | tee $_filenameLogBase

#copy base log file to log file TS
cp $_filenameLogBase $_filenameLogTS

 

MS Windows

Here are good MS Windows version of tee

  1. Google Code Archive
    • wintee
      • Links

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s