Linux - Creating Log Files (2>&1)
Redirect stdout and stderr to file
The
# ... > file 2>&1
For larger packages, it is convenient to create log files
instead of staring at the screen hoping to catch a particular error or warning.
Log files are also useful for debugging and keeping records.
The following command allows you to create an installation log.
Replace with the command you intend to execute.
( 2>&1 | tee compile.log && exit $PIPESTATUS )
2>&1 redirects error messages to the same location as standard output.
The tee command allows viewing of the output while logging the results to a file.
The parentheses around the command run the entire command in a subshell
and finally the exit $PIPESTATUS command ensures the result of the is returned as the result
and not the result of the tee command.
This will run tar czvf /usr/local/backups/daily/etc.tar.gz /etc at 3:12am every day.
The >> /dev/null 2>&1 part means to send any standard output to /dev/null.
and to redirect standard error (2) to the same place as the standard output (1).
Basically it runs the command without any output to a terminal etc
12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
The
2>&1
says make STDERR point to the same places as STDOUT. Since STDOUT is open already, and the shell has done a seek to the end, STDERR will also be appended to STDOUT.# ... > file 2>&1
For larger packages, it is convenient to create log files
instead of staring at the screen hoping to catch a particular error or warning.
Log files are also useful for debugging and keeping records.
The following command allows you to create an installation log.
Replace with the command you intend to execute.
( 2>&1 | tee compile.log && exit $PIPESTATUS )
2>&1 redirects error messages to the same location as standard output.
The tee command allows viewing of the output while logging the results to a file.
The parentheses around the command run the entire command in a subshell
and finally the exit $PIPESTATUS command ensures the result of the is returned as the result
and not the result of the tee command.
This will run tar czvf /usr/local/backups/daily/etc.tar.gz /etc at 3:12am every day.
The >> /dev/null 2>&1 part means to send any standard output to /dev/null.
and to redirect standard error (2) to the same place as the standard output (1).
Basically it runs the command without any output to a terminal etc
12 3 * * * root tar czf /usr/local/backups/daily/etc.tar.gz /etc >> /dev/null 2>&1
Comments
Post a Comment