Operating System - HP-UX
1839322 Members
2945 Online
110138 Solutions
New Discussion

Re: Script output to a log file

 
MikeL_4
Super Advisor

Script output to a log file


I have a script that runs to check some files, etc...

Is there a way to have all output from the script put into a log file ?? now when you run it just scrolls onto the screen..

Thanks
8 REPLIES 8
Massimo Bianchi
Honored Contributor

Re: Script output to a log file

Hi,
redirect the output:


yourscript.sh >/dir/logoutput.log 2>&1

>/dir/logoutput.log : redirects standard output there
2>&1 : redirect standard error

HTH,
Massimo
Alzhy
Honored Contributor

Re: Script output to a log file

Oh simply:

unixprompt# scriptname.ksh | tee script.log

It'll run your script displaying output while at the same time capturing output to a logfile named script.log.

Hakuna Matata.
Chris Wilshaw
Honored Contributor

Re: Script output to a log file

If you wish to capture any user input as well, you can use the script command;

script /tmp/output.log
exit (to complete script logging when your script is finished).
Robert-Jan Goossens
Honored Contributor

Re: Script output to a log file

Hi,

It is a bit cryptical :-) but take a look at the script command.

# script output_file
# your_script


Hope this helps,
Robert-Jan
Franky_1
Respected Contributor

Re: Script output to a log file

Hi,

easy just redirect the output to your desired logfile

"your_script" > "logfile"

eg

script.sh > /tmp/output.log

Regards

Franky
Don't worry be happy
Muthukumar_5
Honored Contributor

Re: Script output to a log file

You can redirect the outputs of a shell script to a log file as,

redirection with redirectors 1> 2>
where 1 - stdout
2 - stderr

Example: ksh test.ksh 1>test.log 2>test.log
or
ksh test.ksh 1>test.log 2>&1

Using script,
script outputfile
ksh test.ksh

exit to stop the script

Using tee command
ksh test.ksh | tee output file

Tee will stop upon exiting from shell script.


You can scroll the log files in parralel as tail -f

And more important;
you can use debugging mode as,
set -x or set -v
on start of script to keep the track of execution of shell sctipts

_muthu_
Easy to suggest when don't know about the problem!
Robert Wells_3
New Member

Re: Script output to a log file

G'day,

I like using the stderr duplication and then using tee, e.g. for bash, ksh or zsh:

my_script 2>&1 | tee my_script_output

Alternatively, I like creating a 'write to a log file' function within the script and then tailing this log file in another window. This lets me create a nice timestamp and maybe error/warning/info level for the log messages.

I usually make log_detailed and log_summary summary functions. These create two different log files; a detailed log and a summary log. The detailed log file contains all the messages that went into the summary log file along with (funnily enough (-: ) more detailed messages.

I have written vim syntax files to vary the highlighting and colours used based on the error levels. These allow a developer to easily see exceptions and also to distinguish between messages from our middleware and messages from the application that they have written.

Hope this helps.

regards,
Rob
Rick Garland
Honored Contributor

Re: Script output to a log file

The "tee -a" option can be very useful for capturing output.

When you invoke your script, pipe to tee -a as well.

Define your $LOG_FILE and then pipe the command to tee -a $LOG_FILE