# unix : redirecting standard error to a file and the terminal.



## cgjoker (Aug 13, 2003)

Hi there.

Is it possible to redirect standard error to a file as well as the terminal/screen?

So I have this command...

script.ksh 2>error.log 1>out.log

I want the log and output to go to a file and to the terminal.

??

thanks.


----------



## AGCurry (Jun 15, 2005)

script.ksh 2>&1 1>out.log | tee error.log


----------



## cgjoker (Aug 13, 2003)

no buts.. got it , thanks for your help.


----------



## nannerphone (Feb 17, 2006)

pipping (|) is your best friend in unix/linux


----------



## cgjoker (Aug 13, 2003)

Hey thanks for the assistance but it still isn't working quite right.

I am not getting the output sent to the terminal. It is only going to the file. 

Here is my excution....

./script1.ksh script2.ksh

script1.ksh:
script=$1
${script} 2>&1 1>> out.log | tee -a error.log

script2.ksh:
echo "success"


Both files are created and capturing output and errors, but the terminal isn't getting the output. The error is I going to the terminal.


----------



## bpmurray (Jun 3, 2003)

If you want everything in one file, try this:


```
echo "Success" 2>&1 | tee -a log.file
```
The problem is the

```
1>>out.log
```
 since this forces eveything to the file without tee'ing it.


----------



## AGCurry (Jun 15, 2005)

cgjoker said:


> Both files are created and capturing output and errors, but the terminal isn't getting the output. The error is I going to the terminal.


"I going to the terminal"? What does that mean?

I thought you wanted, in essence, to swap stdout and stderr. You want BOTH stdin and stderr to go to the terminal, but stdout to go to one file and stderr to the other?

Short of using a named pipe with a catcher process on the other end, I don't see how it's possible. The clunky - but doable - solution is to modify the script to do what you want.


----------



## cgjoker (Aug 13, 2003)

let me recap... id like the standard out and error to be displayed on the terminal while at the same time the output goes to an ouput log and the error to a different error log.

i have this which puts output to the terminal, and sends the output to a file and error to another file. so the only thing missing from this equation is the error to the terminal.

1>&1 2>>error.log 1>>out.log


----------



## bpmurray (Jun 3, 2003)

> 1>&2 2>>error.log 1>>out.log


This won't work because you first say you want stdout to go to stderr, and then you wand stderr to go to error.log but stdout (which is now the same as stderr) to out.log. So the solution is to first send your error output to the error log, and try something like:


```
<your script> 2>error.log | tee -a out.log | cat - out.log
```
However, this won't give you real-time listing of your errors. Perhaps a better solution is to use a script like this:


```
echo "" >error.log
echo "" >out.log
tail -f error.log&
tail -f out.log&
$* 1>>out.log 2>>error.log
```
Call this executeit.ksh and set it to executable (chmod +x). Then run it with your usual stuff as parameters:


```
executeit.ksh skript.ksh
```


----------



## cgjoker (Aug 13, 2003)

while your script makes sense, unfortunately that isn't an option for me. I need to be able to put it into one line. 

you mentioned that im putting stdout into file descriptor stderr.. in fact im putting out to file discriptor out...

1>&1 2>>error.log 1>>out.log

this does in fact seem to work.. but like i said, i would also like error to go to the terminal as well as the out, which right now, only the out goes to the terminal.

any other ideas?


----------



## AGCurry (Jun 15, 2005)

I think you just need to accept that it can't be done without some external machinations.


----------



## cgjoker (Aug 13, 2003)

Well if it can't be done then it cant be done.

Thanks as always for all your help peoples.


----------

