how to use tee without pipes - linux

I want to redirect a program's stdout and stderr to a file and print it to screen at the same time, the obvious solution is to use tee:
./my_program 2>&1 | tee log_file
However, because tee isn't activate until the program finishes execution, I can't see the program output in real-time.
Is there a way to have the same effect while being able to see what a program prints in real-time ?

Yes, redirect to a file directly and use tail -f to follow the output.
First Terminal Window
./my_program 2>&1 > log_file
Second Terminal Window
tail -f log_file

Related

Display the output on the terminal and tail the last 10 lines to log file

I need to run a script and output the full contents to the terminal.
I then want to get the last 10 lines from the terminal output and put them in a log file.
I have tried using ./script.sh 2>&1 | tail -10 > log.log
but this stops the output to the terminal.
Leverage process substitution of bash with tee:
./script.sh |& tee >(tail -10 >file.txt)
|& is a shortcut for sending both STDOUT and STDERR over the pipe.
tee redirects it's STDIN to both STDOUT and to the file(s) given as argument(s) -- we have used process substitution to get a file descriptor and used tail -10 >file.txt inside process substitution to save the desired content.
For that you have to use the tee command. Then you can pipe to a file and your console.
ls -a | tee output.file

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

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

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

What is meant by 'output to stdout'

New to bash programming. I am not sure what is meant by 'output to stdout'. Does it mean print out to the command line?
If I have a simple bash script:
#!/bin/bash
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello'
it outputs a string to the terminal. Does this mean it's 'outputting to stdout' ?
Thanks
Yes, stdout is the terminal (unless it's redirected to a file using the > operator or into the stdin of another process using |)
In your specific example, you're actually redirecting using | grep ... through grep then to the terminal.
Every process on a Linux system (and most others) has at least 3 open file descriptors:
stdin (0)
stdout (1)
stderr (2)
Regualary every of this file descriptors will point to the terminal from where the process was started. Like this:
cat file.txt # all file descriptors are pointing to the terminal where you type the command
However, bash allows to modify this behaviour using input / output redirection:
cat < file.txt # will use file.txt as stdin
cat file.txt > output.txt # redirects stdout to a file (will not appear on terminal anymore)
cat file.txt 2> /dev/null # redirects stderr to /dev/null (will not appear on terminal anymore
The same is happening when you are using the pipe symbol like:
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello'
What is actually happening is that the stdout of the wget process (the process before the | ) is redirected to the stdin of the grep process. So wget's stdout isn't a terminal anymore while grep's output is the current terminal. If you want to redirect grep's output to a file for example, then use this:
wget -q http://192.168.0.1/test -O - | grep -m 1 'Hello' > output.txt
Unless redirected, standard output is the text terminal which initiated the program.
Here's a wikipedia article: http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29

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