Is there a way to tee stderr from a command that's redirecting error to a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is there a way to tee stderr from a command that's redirecting error to a file?
# 1  
Old 09-22-2009
Is there a way to tee stderr from a command that's redirecting error to a file?

I'm not a complete novice at unix but I'm not all that advanced either. I'm hoping that someone with a little more knowledge than myself has the answer I'm looking for.

I'm writing a wrapper script that will be passed user commands from the cron...

Ex:
Code:
./mywrapper.sh "/usr/bin/ps -ef | grep foo > log/ps.out 2>&1"

The wrapper will execute them from the cron and log start and stop times and any errors or exit codes.

Question:
However in the case as above that a user is redirecting their errors to their own log file is there a good way to sample the error such that both the user log and the wrapper log get the error message?

Let me know if you need more info, thanks!

Currently I am grabbing the stderr away from the user command and sending it to a temp file. However this method removes the error from the user log.

Ex:
Code:
eval "$1 2> temp.err"
ERR=`cat temp.err`
rm temp.err


Last edited by sumgi; 09-22-2009 at 02:05 PM..
# 2  
Old 09-24-2009
I have come to a solution but I doubt I will use it since it involves editing users commands dynamically.

My solution comes in two parts:

1. Use a recursive function to track down any error redirection in the issued command:

  • Return nothing if redirection is not taking place
  • If redirection is to another channel (Ex: Stdout is channel 1) call the function again on the new channel, if the function returns nothing pass the channel otherwise pass the returned value
  • Return the file being redirected to if there is one
  • Return null if the user is redirecting to /dev/null or has turned off that channel(Ex. #>&-)

2. Use named pipes created from the mkfifo command to redirect errors from the users command to both the user file and a temp file to use by the wrapper.


Then when I think about redirection happening before a pipe

Ex:
Code:
du -sk /path/* 2> log/du.err | sort -rn > log/sort.log 2>&1

Now I would need to run the command through a loop and do this process for each piped command, creating a named pipe for each redirection.

If someone sees an easier way great but this is the only way I see to do it. It's my opinion that telling users the errors will be located in a master log file will be easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Confused about redirecting stderr

I know that mmmmm 2> error.txt will send the error message to the specified file instead of the screen. However, I have seen >&2 in some scripts, and I can't get it to do anything. A source said it sends stdout and stderr to a file. What file? Ubuntu 18.04.2; Xfce 4.12.3;... (11 Replies)
Discussion started by: Xubuntu56
11 Replies

2. Shell Programming and Scripting

Help required with Stderr and tee command

Hello All, I have a requirement to redirect stdout and stderr to 'log' file and stderr alone to 'err' file. Can someone please help me with this? Thanks in advance (2 Replies)
Discussion started by: vikas_trl
2 Replies

3. Shell Programming and Scripting

Redirecting STDERR to file and screen, STDOUT only to file

I have to redirect STDERR messages both to screen and also capture the same in a file but STDOUT only to the same file. I have searched in this formum for a solution, but something like srcipt 3>&1 >&2 2>&3 3>&- | tee errs doesn't work for me... Has anyone an idea??? (18 Replies)
Discussion started by: thuranga
18 Replies

4. Shell Programming and Scripting

redirecting screen to file without tee

Hi All, I need to redirect screen o/p of shell script to file , but I don't want to use "tee" :Dcommand ./test.sh 2>&1 | tee /var/tmp/testing`date +'%d%h%Y%T'`.log but I want to write some code inside the shell script itself so wen I execute ./test.sh it will display o/p in screen... (3 Replies)
Discussion started by: tarunn.dubeyy
3 Replies

5. Shell Programming and Scripting

STDERR to file & terminal using tee

Hi All, Solarix/Bash v3x Im trying to output any standard errors created by the script to a file using the below command: . runDTE.sh 2> "$DTE_ERROR_FILE" however the errors do get written to the dir/file stored in $DTE_ERROR_FILE but the error does not appear on the terminal screen in... (4 Replies)
Discussion started by: satnamx
4 Replies

6. Shell Programming and Scripting

Redirecting STDERR to a file from within a bash script

I am trying to redirect the output from stderr to a log file from within a bash script. the script is to long to add 2> $logfile to the end of each command. I have been trying to do it with the command exec 2> $logfile This mostly works. Unfortunately, when a read command requires that anything be... (5 Replies)
Discussion started by: vockleya
5 Replies

7. Shell Programming and Scripting

How to use tee with stdout and stderr?

I have been doing this: make xyz &> xyz.log &; tail -f xyz.log The problem with this is that you never can ge sure when "make xyz" is done. How can I pipe both stderr and stdout into tee so both stderr and stdout are copied both to the display and to the log file? Thanks, Siegfried (3 Replies)
Discussion started by: siegfried
3 Replies

8. Shell Programming and Scripting

Redirecting stderr problem

% ls -ld /usr /foo ls: /foo: No such file or directory drwxr-xr-x 14 root wheel 512 May 18 02:49 /usr % ls -ld /usr /foo 1>/dev/null/ /dev/null/: Not a directory. % ls -ld /usr /foo 2>/dev/null/ /dev/null/: Not a directory. ^^Why why why doesn't this work for me. Furthermore, where is... (7 Replies)
Discussion started by: phpfreak
7 Replies

9. Shell Programming and Scripting

Redirecting STDERR message to STDOUT & file at same time

Friends I have to redirect STDERR messages both to screen and also capture the same in a file. 2 > &1 | tee file works but it also displays the non error messages to file, while i only need error messages. Can anyone help?? (10 Replies)
Discussion started by: vikashtulsiyan
10 Replies

10. Shell Programming and Scripting

Redirecting stderr while live

If I forget to set up stderr redirection on execution of a script, is there a way to set that redirection post-exec? In other words, if I have a script running and no errors are being logged... and then I remember that I forgot the 2>&1 on the script... can I turn it on after the fact? ...and... (1 Reply)
Discussion started by: jjinno
1 Replies
Login or Register to Ask a Question