Logging an Entire Session or Batch Job
Problem
You need to capture all the output from an entire session or a long batch job.
Solution
There are many ways to solve this problem, depending on your needs and environment.
The simplest solution is to turn on logging to memory or disk in your terminal program. The problems with that are that your terminal program may not allow that, and when it gets disconnected you lose your log.
The next simplest solution is to modify the job to log itself, or redirect the entire thing to tee or a file. For example, one of the following might work:
$ long_noisy_job >& log_file $ long_noisy_job 2>&1 | tee log_file $ ( long_noisy_job ) >& log_file $ ( long_noisy_job ) 2>&1 | tee log_file
The problems here are that you may not be able to modify the job, or the job itself may do something that precludes these solutions (e.g., if it requires user input, it could get stuck asking for the input before the prompt is actually displayed). That can happen because STDOUT is buffered, so the prompt could be in the buffer waiting to be displayed when more data comes in, but no more data will come in since the program is waiting for input.
There is an interesting program called script that exists for this very purpose and it’s probably already on your system. You run script, and it logs everything that happens to the logfile (called a typescript) you’ve given it, which is OK if you want to log the entire session—just start script, then run your job. But if you only want ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access