I know I can use script to capture everything written in a Terminal window, as long as I run a command such as:

export NOW=$(date +%Y%m%d-%H%M%S)
/usr/bin/script ~/history/output/$NOW.shell.out

The biggest problem here is it requires manual intervention every time a Terminal window is opened - which can be easily forgotten when in a rush with someone pushing from over your shoulder. If I try putting those commands at the end of .bashrc, the output file is created and closed when .bashrc finishes running, terminating the script execution. Putting & at the end of the line to run it in the background leaves script running, but it's disconnected from the terminal, so nothing is written to it.

I also know that I can use, e.g., Select All from Terminal's Edit menu to copy from the scrollback buffer and write that to a file - unless there's a failure. Failure modes I've encountered include:

  1. Typing exit at a command prompt when you think you're in a subshell - so the scrollback buffer goes away when Terminal exits.
  2. Terminal crashes (e.g., from having an unlimited scrollback buffer and memcpy fails, resulting in a SEGFAULT) so the scrollback buffer for every window that was open goes away
  3. Hitting the Terminal Close button when you meant to hit the Close button for MidnightCommander that a supervisor thought would be a better tool - and the scrollback buffer goes away
  4. The laptop battery runs down, the laptop shuts off, and several Terminal scrollback buffers go away at the same time

I've tried configuring Terminal to run a custom command when it starts, but nothing like this has worked:

export NOW=$(date +%Y%m%d-%H%M%S); /usr/bin/script /path/to/home/history/output/$NOW.shell.out
bash | tee /path/to/home/history/output/$(date \+\%Y\%m\%d-\%H\%M\%S).shell.out

(In the latter case, when I got something close to working, I ended up with a file named $(date in the output directory - but it was and remained a zero-length file.)

What I'm looking for is a way to launch something like script automatically every time a login session is started (e.g., when a Terminal window is opened) that will write a uniquely-named log file to my output directory so I have a better chance of avoiding data loss when Terminal encounters an error that prevents me from saving the scrollback buffer.


Put the two commands you specified at the top of your question into a shell script. Put the script for example in /path/to/home/bin/recorder (of course the file needs to be executable and don't forget to insert #!/bin/sh at the beginning).

Then edit the default profile in your gnome-terminal and set the command to run at startup to /path/to/home/bin/recorder, then set the option of "what to do when command ends" to "Keep the terminal open" (sorry, I'm using a different language version of Ubuntu, so the actual English descriptions visible in yoour window may be a bit different). I just checked that and it worked for me.

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.