Solved

redirection of stdout & stderr

Posted on 2001-08-08
8
1,351 Views
Last Modified: 2012-06-27
I've been trying to redirect output of errors from a script job (through cron) to mailx. This works fine, but I loose the stderr going out to the logfile. Ideally I'd like to get stderr & stdout to a log file AND stderr on it's own to mailx.
e.g.
/tmp/test > /tmp/test.log 2 > 'mailx root'

I've been playing with variations of using "tee" to output to two destinations, but no joy...
I've also tried to manipulate file descriptors but still no joy...
e.g.
/tmp/test > /tmp/test.log 3>&2 2>&1 3 > 'mailx root'

am I going in the right direction or lost altogether ?
0
Comment
Question by:ferguswilson
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 5

Expert Comment

by:ecw
ID: 6367753
/tmp/test 2>&1 >/tmp/test.log | mailx root
0
 

Author Comment

by:ferguswilson
ID: 6368768
ecw,
your suggestion is doing the same thing
it just directs the errors FROM the logfile TO mailx
0
 
LVL 5

Expert Comment

by:ecw
ID: 6375616
If you don't mind using a tempfile, maybe ...

  (/tmp/test 2>&1 1>&3 | tee /tmp/test.err) > /tmp/test.log 3>&1 ; test -z /tmp/test.err || mailx root < /tmp/test.err

0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 

Author Comment

by:ferguswilson
ID: 6383407
not really, temp files wouldn't really be a good idea. If I was to go down the road of extra files, I'd be taking a different route than this.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 6383508
(script > logfile) 2>&1|tee -a logfile|mailx root
0
 
LVL 5

Expert Comment

by:paulqna
ID: 6383726
Basically you can:

  1.redirect stdout to a file
  2.redirect stderr to a file
  3.redirect stdout to a stderr
  4.redirect stderr to a stdout
  5.redirect stderr and stdout to a file
  6.redirect stderr and stdout to stdout
  7.redirect stderr and stdout to stderr

Consider this:
exec 3>&1; script 2>&1 1>&3 3>&- | mailx root 1>&2 3>&-

Normal output would be unaffected. The closes there were in case something really cared about all its FDs. We send stderr to sed, and then put it back out 2.

Or did I misunderstood the original question ? ;)
0
 

Author Comment

by:ferguswilson
ID: 6383829
ahoffmann,
this done the trick for me, thanks.
for the record though, it sent an email even if there wern't any errors going to stderr;
e.g.
Your "cron" job
(/sys/test > /sys/test.log) 2>&1|tee /sys/test.log|mailx root
 produced the following output:
UX:mailx: WARNING: No message !?!  Message not sent

...but putting in || at the end resolved it
(/sys/test > /sys/test.log) 2>&1|tee /sys/test.log||mailx root

thanks for your help,
Fergus.
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6383935
aah, nice trick: ||
You got used to shells ;-)
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question