How can I automatically record the terminal session every time a new terminal window is opened?

Viewed 206

I have a script I would like to run when a terminal is opened. This will use the script command to record the terminal session, to aid in troubleshooting the system after some software work is done. When I placed the script in .bash.d, and opened a terminal, it gets stuck in a loop repeatedly starting the script.

script -a -f ~/user_data/error_logs/terminal_output/typescript.$(hostname).$(date -I).txt

2 Answers

I was able to fix it by placing the following in the .bashrc

test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (/data/file_upload/term_log.sh)

The contents of term_log.sh are:

script -a -f -q ~/user_data/error_logs/terminal_output/typescript.txt
kill -9 $PPID

I added the kill -9 $PPID so that when a user typed exit it would close the terminal and not just quit the script.

I have never used bash.d. But I put commands I would like to run at the start of terminal or more specifically bash in ~/.bash_profile and it works like charm.

You will have to close the bash session and start a fresh session for the command you put in ~/.bash_profile to take effect, as commands in ~/.bash_profile are executed only when the bash session starts. Alternatively, after you have put the command in ~/.bash_profile, you can run source ~/.bash_profile for the command to take effect.

Related