Stefan
Stefan

Reputation: 9339

Redirect all output to a file using Bash on Linux

I am trying to redirect all output from a command line programme to a file. I am using Bash. Some of the output is directed to a the file, but some still appears in the terminal and is not stored to the file.

Similar symptoms are described here:

Redirect all output to file in Bash

However, I have tried the proposed solution (capture standard error) without success:

<command> <arguments>   > stdout.txt 2> stderr.txt

The file stderr.txt is created, but it is empty.

A possible clue is that the command-line programme is a client communicating with a server on the same machine. It may be that some of the output is coming from the server.

Is there a way to capture all the output from the terminal, irrespective of its origin?

I've confirmed that the missing output is generated by the server. Running the command in a separate terminal causes some output in both terminals, I can pipe all the output from the command terminal to a file. This raises issues about how to capture the server output, but that's a different question.

Upvotes: 158

Views: 230975

Answers (6)

Patrick Pijnappel
Patrick Pijnappel

Reputation: 7665

Though not POSIX, Bash 4 has the &> operator:

command &> alloutput.txt

Upvotes: 180

ThorSummoner
ThorSummoner

Reputation: 18169

I had trouble with a crashing program cough PHP cough. Upon crashing, the shell it was ran in reported the crash reason, "Segmentation fault (core dumped)".

To avoid this output not getting logged, the command can be run in a subshell that will capture and direct this kind of output:

sh -c 'your_command' > your_stdout.log 2> your_stderr.err
# or
sh -c 'your_command' > your_stdout.log 2>&1

Upvotes: 7

Morlock
Morlock

Reputation: 7131

The proper answer is on ssh - How to pipe output from local to remote server:

your_command | ssh username@server "cat > filename.txt"

Upvotes: 2

You can use this syntax to redirect all output, standard error and standard output, to file stdout.txt:

<command> <arguments> > allout.txt 2>&1

Upvotes: 179

chovy
chovy

Reputation: 75804

You can execute a subshell and redirect all output while still putting the process in the background:

( ./script.sh blah > ~/log/blah.log 2>&1 ) &
echo $! > ~/pids/blah.pid

Upvotes: 6

Brian Campbell
Brian Campbell

Reputation: 333146

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.

Upvotes: 82

Related Questions