Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What I want to achieve is be able to record my terminal sessions to file automatically whenever I use Yakuake/Konsole.

It's easy to achieve if at the start of my session I do:

script -f /home/$USER/bin/shell_logs/$(date +"%d-%b-%y_%H-%M-%S")_shell.log

But I want to run the above automatically whenever I start Yakuake or open a new tab.

Using .bashrc does not work because it creates endless loop as 'script' opens a new session, which in turn reads .bashrc and starts another 'script' and so on.

So presumably I need to script Yakuake/Konsole somehow to run 'script' once as a new tab gets opened. The question is how?

share|improve this question
    
try the problematic solution with the loop problem, but prepend the exec at the start of the line. it should start the script -f in the same shell PID. –  Hanan N. Nov 29 '11 at 22:28
add comment

2 Answers

I figured it out myself :)

So if someone wants to record their terminal sessions automatically (including SSH sessions(!)) using script utility here is how:

Add the following line at the end of /etc/bash.bashrc file (or to .bashrc in your home if you only want your own sessions to be recorded):

test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f $HOME/$(date +"%d-%b-%y_%H-%M-%S")_shell.log)

(we test for shell's parent process not being script and then run script)

That's all! (assuming you have script installed, of course - script is part of bsdutils package) Now when you open new terminal you'll see:

Script started, file is /home/username/file_name.log

script will write your sessions to a file in your home directory naming them something like *30-Nov-11_00-11-12_shell.log*. Adjust this part to suit yourself - for example, you can append your sessions to one large file rather than creating a new one for every session with script -a /path/to/single_log_file; or you can adjust where the files are written to - say, to /var/log/script/$USER_$(date +"%d-%b-%y_%H-%M-%S")_shell.log (make sure you've actually created /var/log/script and made it writable by others).

share|improve this answer
    
You can edit your question and its title yourself. Just click on the edit button that appears right below it. –  Mat Nov 30 '11 at 6:51
2  
You might want to consider adding a ${RANDOM} and/or $$ to the file name since starting two shells within a second of each other will cause a file-name collision. Personally, i often use script.$(date -u +%Y%m%dt%H%M%S).${HOSTNAME:-$(hostname)}.$$.${RANDOM}.log to ensure the files are automatically sorted by date/time and they are consistent accross TZ, i know the host that initiated it, i know the owning process, and there are no name collisions. I rarely use ${USER} because it's typically something for only me. –  nicerobot Dec 2 '11 at 16:33
add comment

The problem with it is, that when you do $ exit, it only exits the script recording...

not the shell

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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