I have a console application that needs to be run as part of the deployment of a new application version on my server.

This console application is designed to output to the console, and cannot be changed.

I'd like to run it as normal, but have stdout and stderr logged to a file at the same time they're output to the console.

How can I do that on Linux?

  • 1
    Search for tee, plenty of answers. – Matt Sep 24 '13 at 9:35
  • 1
    another way would be to do: script -a the_logfile #and then run the apps from the 'script' environment, which will log also special characters, ctrl codes, etc, and could be used to "replay" the display if you manage to use it properly, ie watching the log from the same type of terminal and usnig the same lines/columns, and using a "more" or "less" that outputs control chars (ex: more -v the_logfile) – Olivier Dulac Sep 24 '13 at 15:28
  • Is this an application that prints a stream of text, or a full-screen text-mode application? – Gilles 'SO- stop being evil' Sep 24 '13 at 20:57
  • @Gilles Just a stream of text! – Benjamin Sep 24 '13 at 21:07

You can use tee. For example:

ls -l / | tee tmp.txt

Will print to stdout, and tmp.txt will contain a copy of the output. If you want to include stderr in tmp.txt:

ls -l / 2>&1 | tee tmp.txt
| improve this answer | |
  • Great thanks, just tried it but it looks like it's logging only stdout, is it possible to log stderr as well? – Benjamin Sep 24 '13 at 9:37
  • @Benjamin That should be cmd 2>&1 | tee .... I edited this in. – goldilocks Sep 24 '13 at 11:58
  • Thanks, 2>&1 works fine! – Benjamin Sep 24 '13 at 15:36

You can use tee http://en.wikipedia.org/wiki/Tee_(command)

To pipe only stdout

cmd | tee log.txt | less

To pipe both stdout and stderr:

cmd >>(tee stdout.log) 2>>(tee stderr.log >&2)
| improve this answer | |
  • 1
    Should the > > be >>? – Supr Sep 24 '13 at 13:55
  • Thanks. Will prefer @goldilocks's answer though because I want both stdout and stderr in the same file, just like I would see them on the console! – Benjamin Sep 24 '13 at 15:37

There are several ways to do this.

  1. nohup 2>&1 application &. This will send all output to a file called nohup.out. It will also capture SIGHUPs. So you can close the shell and it will keep running. If you wish tyo see what is happening then you can follow the output with tail -f nohop.out.
  2. The tee command will do the same without preventing SIGHUP. C2h and goldilock already mention this.
  3. If you only need limited scrollback, try screen -L. This is explained in more detail in this post.

I realise that screen is a workaround way, but its other features are very nice for deployements.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.