Saving for myself:
To log all bash sessions for all users in Linux, put this at the end of /etc/bash.bashrc:
session_log_path=/some/log/path
if [ ! -d "$session_log_path/$USER/" ]; then
mkdir -p "$session_log_path/$USER/"
fi
if [ -d "$session_log_path/$USER/" ]; then
[[ "$(ps -ocommand= -p $PPID | awk '{print $1}')" = 'script' ]] || { script -f "$session_log_path/$USER/$(date +"%Y-%m-%d_%H-%M-%S.%3N")_shell.log" && exit ;}
fi
Thanks to the following two posts for the details to get the behavior I wanted (that behavior being: to log the sessions, and then exit the parent shell that spawned the script command as expected when I exit the script command, instead of simply exiting the script command and leaving me in the original bash shell):
- How to automatically record all your terminal sessions with script utility
- ‘script’ session logging makes me exit twice
Edited to add a logpath variable at the top for ease of future updates.