LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-01-2014, 10:37 PM   #1
52358
LQ Newbie
 
Registered: Apr 2012
Posts: 8

Rep: Reputation: Disabled
Question How do I automatically record my bash sessions without running into a recursive loop?


I want to run an experiment and record all my bash sessions. To do this I added the following command at the end of my .bashrc:
Code:
script ~/history/$(date +%F_%T.scr)
As you might imagine, because script starts a new bash session, .bashrc gets run again, which runs the command above again, which starts another bash session and so on.

How would I get around that? Here's my proposed solution, that I don't know how to implement: when I start a new bash session with script, I want to set a variable for that session that determines whether it's being recorded and prevent the script command from being executed in that session (using an if statement in .bashrc). I was thinking of using the -c argument of session to start a new bash session and set the variable manually. I can use the -c argument of the bash command to set a variable for that session, but then the session immediately exits, and I would like it to continue with that new variable being set.

That might have sounded confusing, here's what I'm thinking:
Code:
script -c 'bash -c "isRec=1;continue shell somehow?"' ~/history/$(date +%F_%T.scr)
Anyone know how I might accomplish this?
 
Old 09-01-2014, 10:48 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
What are you trying to accomplish? If you're trying to save your history on a per session basis then you only need to modify the HISTFILE environment variable. That is what bash uses to save history.

Code:
export HISTFILE="${HOME}/history/bash_history_$(date +%Y-%m-%d-%s)"
If you're in a session but want to discard the history you can unset the variable.

Code:
unset HISTFILE
SAM
 
1 members found this post helpful.
Old 09-02-2014, 12:51 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
In that case you need to check if the process script is already running, or you can check the SHLVL environment variable, or you can create a lock file somewhere (if it exists you do nothing, if not just create and start script - and at the end you will need to remove that lock file)
 
1 members found this post helpful.
Old 09-02-2014, 10:55 PM   #4
52358
LQ Newbie
 
Registered: Apr 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
What are you trying to accomplish? If you're trying to save your history on a per session basis then you only need to modify the HISTFILE environment variable. That is what bash uses to save history.

Code:
export HISTFILE="${HOME}/history/bash_history_$(date +%Y-%m-%d-%s)"
If you're in a session but want to discard the history you can unset the variable.

Code:
unset HISTFILE
SAM
HISTFILE would work, except I wanted to capture command output as well. I guess I didn't really mention that. But I didn't know about HISTFILE, so thanks, I learned something new!

Quote:
Originally Posted by pan64 View Post
In that case you need to check if the process script is already running, or you can check the SHLVL environment variable, or you can create a lock file somewhere (if it exists you do nothing, if not just create and start script - and at the end you will need to remove that lock file)
The SHLVL variable does exactly what I needed (tell me whether a session is nested). Thank you!
 
Old 09-03-2014, 02:30 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
There's been other posts on this and my comments are similar which is that one can save their history, but the tougher part is that you also want command output.

I personally stay away from the tricks and so forth. The more I try to abstract, the more I find that it takes away from my time which can be spent doing ... what I'm actually supposed to be doing. Therefore I go the tried and true. My terminal recall windows have unlimited scrollback/memory/whatever you call it. I do something which I need to record or remember, I select all and paste it into a TXT file and also edit that file to add comments. Because some stuff is also notes like "I tried this ... but it didn't work ... so I had to reverse it ... and that's what all this garbage is ..."

Further, I do embedded systems and as a result I have consoles, some of them limited in features or whatever. I'm also installing tons and tweaking when I first get going on a system and this is the exact time I need to recall exactly what I did. The point there is I can't afford to install my bang-zoom-automagic script recall; and then only to find that things like busybox acts differently or that certain commands I use in that script aren't on that system, or the fact that I rely on a particular directory structure to save my stuff; etc. Rather the tried and true copy/paste/edit notes method does work and works well. It's really just the awareness that, that activity is important at the present juncture; so that terminal where I ssh to the target, I can copy all and paste it out. Or the terminal where I have minicom open as my serial console, same result.

Sorry, just my $0.02 there.
 
  


Reply

Tags
bash



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using script to automatically record terminal sessions when a user logs in johnbolton Linux - General 10 02-22-2018 08:33 PM
[SOLVED] recursive directory loop CollieJim Linux - Software 10 03-29-2013 05:44 PM
[SOLVED] ubuntu 11.10 automatically running a bash script zeelog Ubuntu 5 09-18-2012 09:25 AM
Desktop link in recursive loop rocket777 Ubuntu 4 04-26-2007 11:36 PM
record of user sessions PirateJack Linux - Newbie 5 04-03-2006 02:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:04 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