[linux] Redirect all output to 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

However I have tried the proposed solution (capture stderr) without success:

<cmd> <args> > stdout.txt 2> stderr.txt

The file stderr.txt is created but 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?

EDIT:

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.

This question is related to linux bash stdout stderr

The answer is


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

I had trouble with a crashing program *cough PHP cough* Upon crash the shell it was ran in reports 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 these kind of output:

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

you can use this syntax to redirect all output stderr and stdout to stdout.txt

<cmd> <args> > allout.txt 2>&1 

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

command &> alloutput.txt


Proper answer is here: http://scratching.psybermonkey.net/2011/02/ssh-how-to-pipe-output-from-local-to.html

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

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to stdout

Redirect echo output in shell script to logfile Reusing output from last command in Bash Running powershell script within python script, how to make python print the powershell output while it is running Scanf/Printf double variable C Python: How to get stdout after running os.system? How to open every file in a folder Redirect all output to file using Bash on Linux? What does it mean to write to stdout in C? logger configuration to log to file and print to stdout How to get Rails.logger printing to the console/stdout when running rspec?

Examples related to stderr

How I can print to stderr in C? Python: How to get stdout after running os.system? Redirect all output to file using Bash on Linux? How to redirect both stdout and stderr to a file How to print to stderr in Python? How to redirect stderr to null in cmd.exe Confused about stdin, stdout and stderr? How can I pipe stderr, and not stdout? How to store standard error in a variable How to redirect and append both stdout and stderr to a file with Bash?