LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How to run 'script' automatically on Konsole/Yakuake startup/new tab? (https://www.linuxquestions.org/questions/linux-software-2/how-to-run-script-automatically-on-konsole-yakuake-startup-new-tab-916167/)

klearview 11-29-2011 06:10 AM

How to run 'script' automatically on Konsole/Yakuake startup/new tab?
 
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:

Quote:

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?

TB0ne 11-29-2011 09:26 AM

Quote:

Originally Posted by klearview (Post 4537221)
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:

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?

Well, if you only ever want the script to run at user login, remove the "#!/bin/bash" from the top of it, and let your user session do it. That way, you can shove it into .profile or .bashrc, and it'll just run once, as everything else in .bashrc does.

You might also look into putting in the system wide profile (usually /etc/profile), that gets executed at each login. You can put things at the bottom, and probably get away with putting the entire script contents in that file, also eliminating the endless-loop problem.

klearview 11-29-2011 02:48 PM

Hello TB0ne,

Thank you for the reply. I'm afraid though that I either do not understand your solution or you misunderstood my question - I do not mean some random script I made up, I'm talking about a rather unfortunately named program named 'script' that is part of bsdutils together with renice, wall, scriptreplay and logger.

The problem, as I see it, is that whenever 'script' is run, it opens another shell. So if I have an instruction in bashrc to run script, this new shell reads it and starts another script, which opens another shell, which opens another script and so on. "Inception".

So basically it seems that I need to have Yakuake/Konsole calling /usr/bin/script instead of /bin/bash when opening a new tab.

Or did I not understand your answer?

10110111 11-29-2011 02:59 PM

As I understood, script "script" launches a new bash shell. What if you start it like "bash --norc `which script` arg1 arg2" instead of "script arg1 arg2"?

klearview 11-29-2011 03:06 PM

Quote:

Originally Posted by 10110111 (Post 4537650)
As I understood, script "script" launches a new bash shell. What if you start it like "bash --norc `which script` arg1 arg2" instead of "script arg1 arg2"?

In this case I get:

Code:

/usr/bin/script: /usr/bin/script: cannot execute binary file

TB0ne 11-29-2011 03:46 PM

Quote:

Originally Posted by klearview (Post 4537645)
Hello TB0ne,
Thank you for the reply. I'm afraid though that I either do not understand your solution or you misunderstood my question - I do not mean some random script I made up, I'm talking about a rather unfortunately named program named 'script' that is part of bsdutils together with renice, wall, scriptreplay and logger.

The problem, as I see it, is that whenever 'script' is run, it opens another shell. So if I have an instruction in bashrc to run script, this new shell reads it and starts another script, which opens another shell, which opens another script and so on. "Inception".

So basically it seems that I need to have Yakuake/Konsole calling /usr/bin/script instead of /bin/bash when opening a new tab. Or did I not understand your answer?

No, I misunderstood...my apologies. I didn't see BSD mentioned, and didn't know there was an actual program named "script", or what it did. So, totally ignore what i said. :)

10110111 11-29-2011 04:12 PM

Well, OK. You can make a simple script like the following, and make terminal emulator run it instead of /bin/bash.
Code:

#!/bin/bash
script arg1 arg2 # do what you want
bash # and run an interactive shell

In this case you wouldn't need to set up .bashrc .

klearview 11-29-2011 07:10 PM

I did it! I did it! On my own!

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

Code:

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

Script started, file is /home/username/file_name.log
What this does:
We test for
Code:

ps -ocommand= -p $PPID
- here we get a parent process of the shell we've started.
Code:

awk '{print $1}'
- we strip all the unnecessary info to end up with 'script'.
If the test is true we exit, if not - we run script:
Code:

script -f $HOME/$(date +"%d-%b-%y_%H-%M-%S")_shell.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).

The thread title is somewhat misleading now as this solution is terminal-agnostic and will work with everything not just Yakuake/Konsole. Should be something like:'How to automatically record all your terminal sessions with script utility'?


All times are GMT -5. The time now is 04:27 PM.