Log all bash sessions for all users

      No Comments on Log all bash sessions for all users

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):

Edited to add a logpath variable at the top for ease of future updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.