Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell script output to console and logfile 2

Status
Not open for further replies.

AlStl

MIS
Oct 2, 2006
83
US
Guys,

I have successfully coded the shell script output to a logfile:

LOG_DIR = /xxx/yyy

exec 1>$LOG_DIR/script_name/$(date +%Y%m%d%H%M).log

This script performs several functions. Mainly inserting data into several Oracle tables. This takes time. I want to see these two things happen. A or B or BOTH

a) Beside stdout to LOG_DIR i also want to see the output on the screen. Reason: At least i will know when this thing ended.

b) Script tells me when it ended something echo on the screen.

Can someone help?

Al
 
man tee

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
For option (b) you could just echo something to standard error (since you are only logging standard output) when the script is done, e.g.:

Code:
print -u2 "job done"      # ksh only

echo "job done" >&2

To use tee as PHV suggested you would need to either redirect the output of your entire script into a tee command rather than using the exec format, or do something like this:

Code:
LOG_DIR = /xxx/yyy

# create a pipe file
mknod /tmp/pipefile.$$ p

# start tee process in background to read it and output contents
# to screen and log file
tee $LOG_DIR/script_name/$(date +%Y%m%d%H%M).log </tmp/pipefile.$$ &

# redirect stdout to the pipe file for remainder of script
exec >/tmp/pipefile.$$

#
# main script here
#

# clean up pipe file
rm -f /tmp/pipefile.$$

Another perhaps neater way is with Ksh Co-processes, and your exact requirements are describe in exercise 9 here:


Bash 4.0 also supports them, but I haven't tried it, and it doesn't look as tidy:


Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
A very simple way:
Code:
LOG_DIR = /xxx/yyy
log=$LOG_DIR/script_name/$(date +%Y%m%d%H%M).log
(
echo "Start job: $(date)"
[i]your script here[/i]
echo "End job: $(date)"
) | tee $log

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Guys,

Thanks a lot of your input. I am going to mull over this and decide the best way forward.

Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top