Redirect the background process output to a pager `less` after finish running not to default terminal

background-processless

By default, if you execute a background process ended with &, like ./test &, the output will be printed in the current terminal.
How can I redirect the output to a pager like less when it finishes running other than output directly?

Best Answer

Simplest way: Redirect the output to a file. When the command has finished executing, view the file in less.

  1. ./test.sh >test.out &
  2. less test.out

This is under some circumstances also useful even if the command is not run in the background, for example if compiling a large project:

$ make >make.out 2>make.err

or

$ make >make.log 2>&1

Then you can, in your own time go through the output to look for errors etc. (or send it to the developer as part of a bug report), without having to scroll up and down in your terminal.

Related Question