Operating System - HP-UX
1834833 Members
2245 Online
110070 Solutions
New Discussion

exec to direct the output to a file

 
SOLVED
Go to solution
Hanry Zhou
Super Advisor

exec to direct the output to a file

I can't remember exactly, but something like "exec ...> /tmp/outputfile".

It will be put in the beging of my script(ksh), then all output of the script will go to /tmp/outputfile.

Please help.
thanks,
none
13 REPLIES 13
Sundar_7
Honored Contributor

Re: exec to direct the output to a file

exec >> /tmp/out.log 2>&1

This wil redirect the stdout and stderr to /tmp/out.log
Learn What to do ,How to do and more importantly When to do ?
Hanry Zhou
Super Advisor

Re: exec to direct the output to a file

Sundar,

It did direct the output to the file, but what if meanwhile I also want the output displayed on the screen at same time?

Thanks!
none
Michael Tully
Honored Contributor

Re: exec to direct the output to a file

If in a script add this:

set -xv
Anyone for a Mutiny ?
curt larson_1
Honored Contributor

Re: exec to direct the output to a file

Sundar_7
Honored Contributor

Re: exec to direct the output to a file

why to complicate things ? :-)

(





) 2>&1 | tee -a /tmp/log
Learn What to do ,How to do and more importantly When to do ?
Abdul Rahiman
Esteemed Contributor

Re: exec to direct the output to a file

Another way keeping the "exec" in the script -
This goes in the script,
exec >logfile 2>&1 | tee

or still another way,
# ./script &

# tail -f logfile

regds,
Abdul.
No unix, no fun
Abdul Rahiman
Esteemed Contributor

Re: exec to direct the output to a file

Oops, my first method won't work, didn't test it thorough..
But still this would be a workaround,
#./myscript; tail -f /tmp/outfile

regds,
Abdul.
No unix, no fun
Muthukumar_5
Honored Contributor

Re: exec to direct the output to a file

we can get the execution of commands and it's operational output using set,tee anc script commands.

If you start the script(s) with set -x or set -v it will give the debugging of commands execution. If you are running only one command as exec .. then put that command in a script file with the set -x or set -v. Execute the script and traverse all the ouput's and error's to a common log file.

IF want to log all operations that are going to be done,then use script or tee command.

If you want to append the messages in a common log file use -a with script or tee command. Use tee more instead of script because it will log some control characters like ^M for removing operational characters.

script will run upto stopping that command. tee will be stopped after the completio n or termination of command.
Easy to suggest when don't know about the problem!
Geoff Wild
Honored Contributor

Re: exec to direct the output to a file

As others have said - tee is the way to go:

For example:

su - prdadm -c "rdist -f $DRPDIR/distfile prdadm"; ) 2>&1 | tee $DRPDIR/drp.log 2>&1 | mailx -s "PRDADM DRP rdist output" gwild@mydomain.com

That will put it in the drp.log file as well as email me.

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Hanry Zhou
Super Advisor

Re: exec to direct the output to a file

Thanks all for the response. points will be followed soon.

But what if I want the log file name to be "/tmp/log.dateandtime",there is command can change the log file name and put the data and time at the end of the log fiile name. What is that command?

thanks,
none
Sundar_7
Honored Contributor
Solution

Re: exec to direct the output to a file

This is how you can do it

(

) | tee /tmp/log.$(date "+%d%b%y")

modify the date command to have the date format as you would like. refer man date.

The above command will name the log file as for ex. /tmp/log.16Jul04
Learn What to do ,How to do and more importantly When to do ?
D Block 2
Respected Contributor

Re: exec to direct the output to a file

how about simply:
/tmp/log.$$

you can list the runs and find the time of the file creations.

ls -l /tmp/log.*


Golf is a Good Walk Spoiled, Mark Twain.
Abdul Rahiman
Esteemed Contributor

Re: exec to direct the output to a file

tee command would work in most cases to send the o/p to stdout as well as append to a file. But I have seen some limitations of tee if your script is extensive and calls other scripts internally or have jobs running in the background etc. I don't remember the exact scenario, but I had fun with tee doing it.

If you use the "exec >$(logfile) 2>&1" method, you can make this as a standard header in most scripts and append/create logiles the you wanted.
I do keep something like this as a header, which is very much reusable.

logdir=/var/adm/log
script=`basename ${0)`
date=`date "+%d-%b-%y:%H:%M"`
logfile=${logdir}/${script}.${date}.log
exec >>${logfile} 2>&1

(
script
)
No unix, no fun