Capture Shell Script Output To A File

Hi,

I am running a shell script called dbProcess.sh which performs shutdown and startup of various Oracle instances we have.At the time of execution the script produces the following output to the command line window

$./dbProcess.sh stop

#### Run Details ######
Hostname : server-hop-1
RunDate : 01-13-09
RunTime : 09:43:14
#### ########### ######
##############################################################################
/u01/app/oracle/product/9.2
/u01/app/oracle
it9
##############################################################################
##############################################################################
# Setting Environment Variables For SID : # it9
##############################################################################
##############################################################################
# Contents Of Connection Out File #
##############################################################################
##############################################################################
# Killing Active DB Connections #
##############################################################################
Connected.
Database closed.
Database dismounted.
ORACLE instance shut down.
##############################################################################

I would like to create an output file being at the time of executing the dbProcess.sh and the process must write all the output lines of the dbProcess.sh to this file.

I would like to clarify that i am not looking for any kind of redirection commands using the > or >> symbol.

What i am looking out for is a series of commands that i will be able to add to my dbProcess.sh shell script which will start a log file write all the shell output to the logfile and at the end of it save and exit the log file. I dont mind if the output is not showing up on the stdout for me most important aspect is the creation of this logfile and to write all output to this logfile.

I tried the to add lines to dbProcess.sh using the unix script command.But it did not work because when the shell script executed the script command line it stopped and waited for me to type exit for it to continue.

Please let me know if you have any clarification.

Thanks and kind Regards,
Rajan.S

Somewhere in the beginning of your file put this

exec 1>/tmp/dbProcess.${$}.log
exec 2>/tmp/dbProcess.${$}.debug

That will effectively redirect STDOUT to a logfile (unique through the processes' ID) and STDERR to a debugging file.

Hi,

The above exec command worked.. Thanks...

What are the commands if we would like to see the output on stdout as well as log to file. Because in case of exec the output is not being shown in the terminal.

Thanks an Kind Regards,
Rajan.S

You cannot do both with just a redirection, because one file descriptor cannot output to more than one stream; it becomes a lot trickier, needing an entire second process to write the extra stream, either that or significant modifications to your original script.

You could try using tee (not available on all *NIXes), but that doesn't work with exec, but would require to change the whole script
Or run your script in background and follow with tail -f