LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-29-2011, 06:10 AM   #1
klearview
Member
 
Registered: Aug 2006
Location: London
Distribution: Debian, Kubuntu
Posts: 572

Rep: Reputation: 75
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?
 
Old 11-29-2011, 09:26 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,613

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by klearview View Post
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.
 
Old 11-29-2011, 02:48 PM   #3
klearview
Member
 
Registered: Aug 2006
Location: London
Distribution: Debian, Kubuntu
Posts: 572

Original Poster
Rep: Reputation: 75
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?

Last edited by klearview; 11-29-2011 at 02:49 PM.
 
Old 11-29-2011, 02:59 PM   #4
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
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"?
 
Old 11-29-2011, 03:06 PM   #5
klearview
Member
 
Registered: Aug 2006
Location: London
Distribution: Debian, Kubuntu
Posts: 572

Original Poster
Rep: Reputation: 75
Quote:
Originally Posted by 10110111 View Post
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
 
Old 11-29-2011, 03:46 PM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,613

Rep: Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962Reputation: 7962
Quote:
Originally Posted by klearview View Post
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.
 
Old 11-29-2011, 04:12 PM   #7
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
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 .
 
1 members found this post helpful.
Old 11-29-2011, 07:10 PM   #8
klearview
Member
 
Registered: Aug 2006
Location: London
Distribution: Debian, Kubuntu
Posts: 572

Original Poster
Rep: Reputation: 75
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'?

Last edited by klearview; 11-29-2011 at 07:16 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Run a script in a new tab nanderh Linux - Newbie 2 01-28-2009 09:27 PM
How to create permanent aliases in yakuake/konsole? Pau Gasol Slackware 6 09-22-2007 01:54 AM
Run program a startup automatically shambler SUSE / openSUSE 2 09-13-2006 10:50 AM
How to automatically run a command when you open a Konsole shazam75 Linux - Software 3 11-30-2004 04:02 AM
How can the linux OS run script automatically at startup? tommylkc Linux - Newbie 1 06-26-2002 03:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration