3

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

3
  • 4
    How do you know it gets stuck in a loop? How did you come to that conclusion? Commented Nov 5, 2018 at 23:40
  • 1
    replace your command with date >> debug.txt or script -a -c date debug.txt and check for a loop.
    – Walter A
    Commented Nov 6, 2018 at 19:58
  • 1
    when I open a new terminal it repeatedly displays in the command line that it is starting the script and I have to ctrl c to stop it. Commented Nov 6, 2018 at 23:33

2 Answers 2

2

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.

1

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.

2
  • Don't forget about the if statement so it doesn't run multiple times if for example source .bash_profile is used. Commented Nov 6, 2018 at 11:56
  • can you clarify "if" statement? Im still learning as I go any help is appreciated! Commented Nov 6, 2018 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.