Redirect all output to file using Bash on Linux?
If the server is started on the same terminal, then it's the server's stderr that is presumably being written to the terminal and which you are not capturing.
The best way to capture everything would be to run:
script output.txt
before starting up either the server or the client. This will launch a new shell with all terminal output redirected out output.txt as well as the terminal. Then start the server from within that new shell, and then the client. Everything that you see on the screen (both your input and the output of everything writing to the terminal from within that shell) will be written to the file.
When you are done, type "exit" to exit the shell run by the script
command.
Redirect all output to file in Bash
That part is written to stderr, use 2>
to redirect it. For example:
foo > stdout.txt 2> stderr.txt
or if you want in same file:
foo > allout.txt 2>&1
Note: this works in (ba)sh, check your shell for proper syntax
How to redirect output to a file and stdout
The command you want is named tee
:
foo | tee output.file
For example, if you only care about stdout:
ls -a | tee output.file
If you want to include stderr, do:
program [arguments...] 2>&1 | tee outfile
2>&1
redirects channel 2 (stderr/standard error) into channel 1 (stdout/standard output), such that both is written as stdout. It is also directed to the given output file as of the tee
command.
Furthermore, if you want to append to the log file, use tee -a
as:
program [arguments...] 2>&1 | tee -a outfile
How to redirect and append both standard output and standard error to a file with Bash
cmd >>file.txt 2>&1
Bash executes the redirects from left to right as follows:
>>file.txt
: Openfile.txt
in append mode and redirectstdout
there.2>&1
: Redirectstderr
to "wherestdout
is currently going". In this case, that is a file opened in append mode. In other words, the&1
reuses the file descriptor whichstdout
currently uses.
redirect all output in a bash script when using set -x
This is what I've just googled and I remember myself using this some time ago...
Use exec to redirect both standard output and standard error of all commands in a script:
#!/bin/bash
logfile=$$.log
exec > $logfile 2>&1
For more redirection magic check out Advanced Bash Scripting Guide - I/O Redirection.
If you also want to see the output and debug on the terminal in addition to in the log file, see redirect COPY of stdout to log file from within bash script itself.
If you want to handle the destination of the set -x
trace output independently of normal STDOUT
and STDERR
, see bash storing the output of set -x to log file.
Shell, redirect all output to a file but still print echo
You can take this approach of duplicating stdout file descriptor and using a custom echo
function redirecting to duplicate file descriptor.
#!/bin/bash
# open fd=3 redirecting to 1 (stdout)
exec 3>&1
# function echo to show echo output on terminal
echo() {
# call actual echo command and redirect output to fd=3 and log file
command echo "$@"
command echo "$@" >&3
}
# redirect stdout to a log file
exec >>logfile
printf "%s\n" "going to file"
date
echo "on stdout"
pwd
# close fd=3
exec 3>&-
How to redirect output of an entire shell script within the script itself?
Addressing the question as updated.
#...part of script without redirection...
{
#...part of script with redirection...
} > file1 2>file2 # ...and others as appropriate...
#...residue of script without redirection...
The braces '{ ... }' provide a unit of I/O redirection. The braces must appear where a command could appear - simplistically, at the start of a line or after a semi-colon. (Yes, that can be made more precise; if you want to quibble, let me know.)
You are right that you can preserve the original stdout and stderr with the redirections you showed, but it is usually simpler for the people who have to maintain the script later to understand what's going on if you scope the redirected code as shown above.
The relevant sections of the Bash manual are Grouping Commands and I/O Redirection. The relevant sections of the POSIX shell specification are Compound Commands and I/O Redirection. Bash has some extra notations, but is otherwise similar to the POSIX shell specification.
Redirect all output in a bash script when using set -x, capture pid and all output
Change 2>&1> /tmp/foo_service.log
to >/tmp/foo_service.log 2>&1
.
You should first redirect fd 1 to the file, then let fd 2 duplicate it. What you're doing on the former is that you first redirect fd 2 to 1 which only copies the default stdout, not the file's fd which is opened after it.
Related Topics
Enable/Disable Tasks in Crontab by Bash/Shell
Gzip Multiple Files Individually and Keep the Original Files
About Fork and Execve System Call
Bash Script Read All the Files in Directory
Sorting Scientific Number with Unix Sort
Does Gcc, Icc, or Microsoft's C/C++ Compiler Support or Know Anything About Numa
Sending Keyboard Input to a Program from Command-Line
How to Capture Network Packets Per Pid
Identify Other End of a Unix Domain Socket Connection
How to Write and Execute a Hello World Program in File for R
Not Authorized for Query on Admin.System.Namespaces on Mongodb
Call to Daemon in a /Etc/Init.D Script Is Blocking, Not Running in Background
How to Fix Permission Denied for .Git/ Directory When Performing Git Push
Hook Functions for Linux Filesystem
Selective Core Dump in Linux - How to Select the Dumped Sections
Linux Perf Reporting Cache Misses for Unexpected Instruction