Reputation: 33
I'm trying to set up a little shell script using the linux command "script" to log every input on my Kali Linux machine.
#!/bin/bash
now=$(date +"%m_%d_%Y_%H:%M:%S")
script /root/Logs/log_$now.txt
The script itself seems to work but i want to add it to the bash autostart, so whenever i open a terminal, my shellscript gets executed.
I tried adding it to my .bashrc file but when I open a terminal now, the script gets looped. I added a simple "echo 'test'" script and it only starts once on terminal launch. Adding the script to my .profile file and executing .profile manually works as intended, but as soon as i enter a script using the "script" command to my .bashrc, it gets looped.
Thank you in advance.
Upvotes: 3
Views: 1671
Reputation: 531175
A new terminal window is one way of starting a new interactive shell, but so is running script
. You only want to run script
in the first case, not in every case.
script
itself sets a variable in the environment to let you know if you are already in a shell started by script
. Check for that variable before trying to run script
again.
if [[ -z $SCRIPT ]]; then
now=$(date +"%m_%d_%Y_%H:%M:%S")
script /root/Logs/log_$now.txt
fi
The value of SCRIPT
, if set, is the name of the file being logged to.
Alternatively, you can configure your terminal emulator to run script
directly, rather than having it continue to open an ordinary interactive shell and you trying to alter its configuration.
The above applies to BSD script
; for GNU script
, you'll have to set such a variable yourself.
if [[ -z $SCRIPT ]]; then
now=$(date +"%m_%d_%Y_%H:%M:%S")
export SCRIPT=/root/Logs/log_$now.txt
script "$SCRIPT"
fi
Upvotes: 5
Reputation: 1739
The script(1) command opens a new interactive shell.
The file .bashrc
runs on every interactive bash shell that is started, hence your infinite recursion.
If you want something to run only on the login shell, you put it into .bash_profile
.
This should avoid the infinite recursion.
Upvotes: 2