Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect both stdout and stderr to a file [duplicate]

I am running a bash script that creates a log file for the execution of the command

I use the following

Command1 >> log_file Command2 >> log_file 

This only sends the standard output and not the standard error which appears on the terminal.

like image 493
sdmythos_gr Avatar asked Sep 23 '11 09:09

sdmythos_gr


People also ask

What is the meaning of 2 >& 1?

1 "Standard output" output file descriptor. The expression 2>&1 copies file descriptor 1 to location 2 , so any output written to 2 ("standard error") in the execution environment goes to the same file originally described by 1 ("standard output").

What is the redirect option to use for sending both standard output and standard error to the same location?

Discussion. &> or >& is a shortcut that simply sends both STDOUT and STDERR to the same place—exactly what we want to do.


1 Answers

If you want to log to the same file:

command1 >> log_file 2>&1 

If you want different files:

command1 >> log_file 2>> err_file 
like image 73
Mat Avatar answered Sep 23 '22 18:09

Mat