simple scripting question - directing output to a log file

Jeffrey Pyne plug-discuss@lists.plug.phoenix.az.us
Tue, 28 Jan 2003 09:37:49 -0700


The simplest way is to use shell redirection in the script:

	mirrordir --exclude /mnt / /mnt/hda4 > /my/log/file

This redirects STDOUT to /my/log/file, truncating the file first.  If you
want to append to the end of the file instead of overwriting it, do this:

	mirrordir --exclude /mnt / /mnt/hda4 >> /my/log/file

If you also want any errors generated by your command written to the log
file, you can do this:

	mirrordir --exclude /mnt / /mnt/hda4 >> /my/log/file 2>&1

The 2>&1 redirects STDERR to STDOUT, which has previously been redirected to
/my/log/file.

If you're doing a lot of redirects in a script, you can use the exec command
to redirect all STDOUT and STDERR, like this:

	#!/bin/bash

	exec >> /my/log/file
	exec 2>> /my/error/file

	mirrordir --exclude /mnt / /mnt/hda4
	another_command
	more_commands
	
This will redirect the output of the rest of the script to /my/log/file, and
redirect the errors generated by the rest of the script to /my/error/file.
This comes in handy when you have lots of commands and you want them all
redirected to one place.

You can also get fancy and write your output to the screen AND a log file
(or two, or three...):

	mirrordir --exclude /mnt / /mnt/hda4 | tee -a /my/log/file
/my/other/file

The output of the mirrordir command appears on your screen, and is also
appended to (-a) /my/log/file and /my/other/file.

~Jeff

On Tuesday, January 28, 2003 9:09 AM, Scott H wrote:

> I have a little script that I want to use to
> mirror one partition to another, using mirrordir
> via a cron job.  I want the script to log it's
> output to a log file, like .../messages, or a
> custom one, like .../mirrordir.log.  How do I get
> it to do this, either in the script itself, or by
> telling cron to put output there?  Here sort of
> what the script looks like:
> 
> #!/bin/sh
> date
> echo "Mirrordir says:"
> mirrordir --exclude /mnt / /mnt/hda4
> date
> echo "Done."
> 
> TIAFAH!
> 
> Scott