Shell – how can i automatically record every terminal session

historylogsshelltypescript

I like to use tools like history, script, and asciinema to record and review xterm (i.e. bash/sh) sessions for typical, general administration reasons, and also, sometimes for comprehensive study/revision; as a learning tool.

  • I thought of setting the default $SHELL as a direct path to said tools and/or commands, like:

    • /bin/script transcript.log;
    • /bin/asciinema rec -w0.5 term_log.json;
  • But these tools typically rely on having a default $SHELL set (i.e. /bin/bash, /bin/sh, /bin/zsh, etc.).

  • Also, I feel like re-defining system variables (i.e. $SHELL, $TERM, etc.) or trying to get it working with config (*.conf) and dot (~/.bashrc / ~/.bash_profile) files is gona be riddled with all kinds of holes and potential problems.

  • However, every time I decide or realize that I want a transcript of a session, it's after the fact, or I'll be halfway through the interesting part already, or some such scenario.

What's the correct, or best practice to set something like this up to simply log everything (preferably stdin, stdout (and stderr when applicable), etc.) automatically?

Best Answer

Something like this in your .bash_profile or .bashrc might work for you:

if test -t 0 -a -t 1 -a -z "$SCRIPTING"
then
    export SCRIPTING="$HOME/.script.$(date +%Y%m%d_%H%M%S).$$)"
    exec script "$SCRIPTING"
fi
Related Question