How can I send error output to both stdout and file in bash - linux

If I use this
cmd 2>/var/error.log
Then my error goes to that file but then I can't see on screen.
Is there any way I can simultaneously show it on screen as well as send to file?

This will display both stdout and stderr on the terminal while only sending stderr to err.log:
cmd 2> >(tee err.log >&2)
>(...) is process substitution. (The space between the two consecutive > is essential.) This sends stderr and only stderr to the tee command.
The >&2 causes the error messages remain in stderr. This would be important, for example, if this line occurs inside some script whose stdin or stderr is being redirected. (Hat tip: Chepner.)

cmd 2>&1 | tee /tmp/error.log

Related

Write stderr and stdout to one file, but also write stderr to a separate file

I have a shell script whose stdout and stderr I want to write to a logfile. I know that this can be achieved via
sh script.sh >> both.log 2>&1
However, I also want to simultaneously write the stderr to a separate file, "error.log". Is this achievable?
You can use tee to duplicate output to two locations. Combine that with some tricky redirections, and...
script.sh 2>&1 >> both.log | tee -a both.log >> error.log
This redirects stderr to stdout, and then stdout to both.log. stderr remains, and is piped to tee, which copies it to both log files.
For this you need to first switch stdout and stderr, which requires an additional file descriptor:
sh script.sh 3>&2 2>&1 1>&3 3>&-
The last operator closes the auxiliary file descriptor.
After that you can use tee to duplicate the error stream (which is now on stdin) and append it to your error log:
sh script.sh 3>&2 2>&1 1>&3 3>&- | tee -a error.log
And after that you can then direct both stdin and stderr to your combined log:
(sh script.sh 3>&2 2>&1 1>&3 3>&- | tee -a error.log) >> both.log 2>&1
The parentheses around the command are important to capture the error stream of the whole command. Without them only the (empty) error stream of the tee command would be captured and the rest would still go to the terminal.
Note: this does not check wheater the file descriptor 3 was in use (open) before. In bash you can use this to choose a previously unused file descriptor and close it on the last redirection:
sh script.sh {tmpfd}>&2 2>&1 1>&${tmpfd}-

Get output on screen from linux command

I tried to get command output in a command but I could't get it. If I run manually, It return normal result in screen.
[root#mymachine ~]# dnsdist -e "topQueries()"
1 Rest 0 100.0%
I tried with dnsdist -e 'topQueries() 2>&1 1> /tmp/abc. But /tmp/abc still was empty.
I don't think screen command can resolve it.
Does anyone have any recommendations?
Addition information:
1 Rest 0 100.0%
is the result from topQueries(). '-e' mean execute a command in dnsdist. It works look like we access mysql then we call a sql statement.
As rkosegi commented, order is important. When you run:
examplecommand 2>&1 1> /tmp/abc
It takes the stderr output and sends it to where stdout is currently going and then sends stdout to the file.
You may want, instead:
examplecommand 1> /tmp/abc 2>&1
which sends stdout to the file, then sends stderr to where stdout is currently going, namely the file.
To have stderr and stdout both to te screen and in a file /tmp/abc you
can use tee.
$cat a.sh
echo "stderr" >&2
echo "stdout"
./a.sh 2>&1 | tee /tmp/abc
stderr
stdout
$ cat /tmp/abc
stderr
stdout

Redirecting stderr in csh

I'm executing a program that dumps crash report into STDERR from where I have to filter some necessary information. The problem is that I'm unable to redirect STDERR to STDOUT and PIPE it with grep
command 2>&1 >/dev/null | grep "^[^-]" >& /tmp/fl
Getting error: Ambiguous output redirect.
Same command works under bash terminal.
What should I change to make it work ?
csh is significantly more limited than bash when it comes to file redirection. In csh, you can redirect stdout with the usual > operator, you can redirect both stdout and stderr with the >& operator, you can pipe stdout and stderr with the |& operator, but there is no single operator to redirect stderr alone.
The usual workaround is to execute the command in a sub-shell, redirecting stdout in that sub-shell to whatever file you want (/dev/null in this case), and then use the |& operator to redirect stdout and stderr of the sub-shell to the next command in the main shell.
In your case, this means something like:
( command >/dev/null ) |& grep "^[^-]" >&/tmp/fl
Because stdout is redirected to /dev/null inside the sub-shell, the |& operator will end up acting as 2>&1 in bash - since stdout is discarded in the sub-shell, nothing written to stdout will ever reach the pipe.
If you dont mind mixing stdout and stderr into the pipe you can use
command |& grep "^[^-]" >& /tmp/fl
Otherwise you can do the hack:
(command >/dev/null) |& grep "^[^-]" >& /tmp/fl
which separates out stdout to null, then piping stdout and stderr just gives
stderr as content.

How to redirect command line outputs to a file, but still show them in the command line?

In tcsh I want to redirect command line outputs to a file, but I still want to show them in the command line.
Did a little bit search that
./MyCommand.sh 2>&1 | tee /tmp/Output.txt
should do the job. But I got an error like:
Ambiguous output redirect
Use of 2>&1 to combine stderr and stdout works only in bash and sh. It does not for csh or tcsh. A work around is suggested at Redirect stdout to stderr in tcsh.
In bash instead of 2>&1 I use |&
Not sure how this plays out for tcsh, but this question isn't currently tagged for it and hoping this helps someone else.
According to this redirect stderr to stdout in c shell you can't do this in csh which tcsh extends which could be related
It isn't clear from the question if you want to redirect stdout only, or stdout and stderr.
Using | will redirect stdout to tee (which outputs it to a file and to terminal), leaving stderr untouched (so it only goes to terminal):
./MyCommand.sh | tee /tmp/Output.txt
Using |& will "merge" stdout and stderr, and tee will redirect both to file and to terminal:
./MyCommand.sh |& tee /tmp/Output.txt

How to log output in bash and see it in the terminal at the same time?

I have some scripts where I need to see the output and log the result to a file, with the simplest example being:
$ update-client > my.log
I want to be able to see the output of the command while it's running, but also have it logged to the file. I also log stderr, so I would want to be able to log the error stream while seeing it as well.
update-client 2>&1 | tee my.log
2>&1 redirects standard error to standard output, and tee sends its standard input to standard output and the file.
Just use tail to watch the file as it's updated. Background your original process by adding & after your above command After you execute the command above just use
$ tail -f my.log
It will continuously update. (note it won't tell you when the file has finished running so you can output something to the log to tell you it finished. Ctrl-c to exit tail)
You can use the tee command for that:
command | tee /path/to/logfile
The equivelent without writing to the shell would be:
command > /path/to/logfile
If you want to append (>>) and show the output in the shell, use the -a option:
command | tee -a /path/to/logfile
Please note that the pipe will catch stdout only, errors to stderr are not processed by the pipe with tee. If you want to log errors (from stderr), use:
command 2>&1 | tee /path/to/logfile
This means: run command and redirect the stderr stream (2) to stdout (1). That will be passed to the pipe with the tee application.
Learn about this at askubuntu site
another option is to use block based output capture from within the script (not sure if that is the correct technical term).
Example
#!/bin/bash
{
echo "I will be sent to screen and file"
ls ~
} 2>&1 | tee -a /tmp/logfile.log
echo "I will be sent to just terminal"
I like to have more control and flexibility - so I prefer this way.

Resources