LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 04-14-2011, 11:40 AM   #1
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Rep: Reputation: 115Reputation: 115
bash command history log - how to write it continuous?


I've been looking for how to set this up in bash with no luck so far. I can change what file the history log is written to, and how much history is saved. But it only writes the saved part when bash exits. Instead, I'd like to have bash write that file continuously as each command is entered (and maybe also do an fsync(2) to flush it to disk). That way I can see the command I crashed the box with Anyone know the magical incantation for that?

Here's a nice page that summarizes bash history settings. But there appears to be nothing that I can find there, in the man page, or other searches, that suggest it even can do continuous.

http://www.cioby.ro/linux/customize-...y-in-linux.php
 
Old 04-14-2011, 12:18 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,521
Blog Entries: 51

Rep: Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599
Works for me:
Code:
~]$ tac .bashrc|head -2|tac
shopt -s histappend
export PROMPT_COMMAND='history -a'
 
1 members found this post helpful.
Old 04-14-2011, 02:00 PM   #3
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by unSpawn View Post
Works for me:
Code:
~]$ tac .bashrc|head -2|tac
shopt -s histappend
export PROMPT_COMMAND='history -a'
The "history -a" step seems to not be working.
Code:
lorentz/phil/t0 /home/phil 1> shopt
autocd          off
cdable_vars     off
cdspell         off
checkhash       off
checkjobs       off
checkwinsize    on
cmdhist         on
compat31        off
compat32        off
compat40        off
dirspell        off
dotglob         off
execfail        off
expand_aliases  on
extdebug        off
extglob         off
extquote        on
failglob        off
force_fignore   on
globstar        off
gnu_errfmt      off
histappend      on
histreedit      off
histverify      off
hostcomplete    on
huponexit       on
interactive_comments    on
lithist         off
login_shell     off
mailwarn        off
no_empty_cmd_completion off
nocaseglob      off
nocasematch     off
nullglob        off
progcomp        on
promptvars      on
restricted_shell        off
shift_verbose   off
sourcepath      on
xpg_echo        off
lorentz/phil/t0 /home/phil 2> ls -l "${HISTFILE}"
-rw-r--r-- 1 phil phil 0 2011-04-14 14:55 /home/phil/.bash_history.d/2011-04-14-145520-02633-lorentz-phil-pts/54
lorentz/phil/t0 /home/phil 3> history -a
lorentz/phil/t0 /home/phil 4> ls -l "${HISTFILE}"
-rw-r--r-- 1 phil phil 0 2011-04-14 14:55 /home/phil/.bash_history.d/2011-04-14-145520-02633-lorentz-phil-pts/54
lorentz/phil/t0 /home/phil 5> bash --version
GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
lorentz/phil/t0 /home/phil 6> ls -l "${HISTFILE}"
-rw-r--r-- 1 phil phil 0 2011-04-14 14:55 /home/phil/.bash_history.d/2011-04-14-145520-02633-lorentz-phil-pts/54
lorentz/phil/t0 /home/phil 7>
And this is under Ubuntu 10.10 if that matters (and I'm worried it might).
 
Old 04-14-2011, 02:24 PM   #4
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
A strange bug just happened ... I click "Reply" here (and I verified it was definitely here because there was a dotted ring around the "Post Reply" button), and entered some text and clicked submit. But it ended up in a different thread I had posted in. I came back and was back here. I've seen that happen on LQ a lot in the past, but for a few months it has not. Now it's back. Maybe there's still a locking or semaphore issue somewhere.

Anyway, I hope it gets in the right place this time.

I was thinking maybe I could do "history > ${HISTFILE}" in the prompt command string instead of "history -a" (which is not doing anything, as described above). But I often have bash shells running for weeks or even months, and have thousands of commands. And it might lose records if the machine crashes right then. This was just a thought.

Edit: seems to be in the right place, now.

Last edited by Skaperen; 04-14-2011 at 02:26 PM.
 
Old 04-14-2011, 04:10 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,521
Blog Entries: 51

Rep: Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599
I'm sorry. I should have told you I have those two lines at the end of my ~/.bashrc.
 
Old 04-15-2011, 06:43 AM   #6
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by unSpawn View Post
I'm sorry. I should have told you I have those two lines at the end of my ~/.bashrc.
I figured that much. I can see it sets up the PROMPT_COMMAND variable so that it executes "history -a" at each prompt. What I'm reporting back is that "history -a" is not writing anything at all to the file named in HISTFILE. Maybe something else besides "shopt -s histappend" also needs to be set to make it work? Maybe Ubuntu broke it (they have broken other things, like the resolver).
 
Old 04-15-2011, 09:13 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,521
Blog Entries: 51

Rep: Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599
Works for me using ancient bash 3.2.25 thought I don't modify my history location or any such defaults. Can't think of any other switches, sorry.
 
Old 04-15-2011, 09:22 AM   #8
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
Do you have HISTFILE actually set, or is it unset and using the default?
 
Old 04-15-2011, 09:56 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,521
Blog Entries: 51

Rep: Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599
Unset using defaults.
 
Old 04-15-2011, 02:12 PM   #10
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
Not working. So I guess bash version 4.1.5(1) in Ubuntu 10.10 is broken.

Last edited by Skaperen; 04-15-2011 at 02:19 PM.
 
Old 04-15-2011, 02:35 PM   #11
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
this approach works

This works. Instead of "history -a" I used "history -w". It says it writes the whole history file, replacing the previous one. It at least accomplishes ALMOST the desired end result. However, strace shows it does O_TRUNC on the original file, rather than write a new file and rename() to replace the link. So there is an ever so slight window of data loss that would not be the case if it really did O_APPEND.

I guess I could add commands in PROMPT_COMMAND to do that operation.

I wonder if between bash 3 and bash 4 they changed the semantics of "history -a" from "do an appending write immediately" to "set a flag so that the history writing when bash exits will open with O_APPEND". This would seem a bizarre change, but I've seen some strange stuff with GNUware.

Last edited by Skaperen; 04-15-2011 at 02:37 PM. Reason: add "I guess ..."
 
Old 04-15-2011, 02:42 PM   #12
Skaperen
Senior Member
 
Registered: May 2009
Location: WV, USA
Distribution: Slackware, CentOS, Ubuntu, Fedora, Timesys, Linux From Scratch
Posts: 1,777
Blog Entries: 20

Original Poster
Rep: Reputation: 115Reputation: 115
So it's a little more bizarre than I had hoped for, but at least it is working:
Code:
lorentz/phil /home/phil 1> env | grep PROMPT_COMMAND
PROMPT_COMMAND=history -w ${HISTFILE}_tmp;mv ${HISTFILE}_tmp ${HISTFILE} ;export PS1="\h/\u `exec pwd` "'\!> '
lorentz/phil /home/phil 2> ls -dil "${HISTFILE}"
65013528 -rw------- 1 phil phil 26 2011-04-15 15:39 /home/phil/.bash_history.d/2011-04-15-153919-17096-lorentz-phil-pts_34
lorentz/phil /home/phil 3> ls -dil "${HISTFILE}"
65013531 -rw------- 1 phil phil 48 2011-04-15 15:40 /home/phil/.bash_history.d/2011-04-15-153919-17096-lorentz-phil-pts_34
lorentz/phil /home/phil 4> ls -dil "${HISTFILE}"
65013528 -rw------- 1 phil phil 70 2011-04-15 15:40 /home/phil/.bash_history.d/2011-04-15-153919-17096-lorentz-phil-pts_34
lorentz/phil /home/phil 5> ls -dil "${HISTFILE}"
65013531 -rw------- 1 phil phil 92 2011-04-15 15:40 /home/phil/.bash_history.d/2011-04-15-153919-17096-lorentz-phil-pts_34
lorentz/phil /home/phil 6>
 
Old 04-15-2011, 02:56 PM   #13
unSpawn
Moderator
 
Registered: May 2001
Posts: 26,521
Blog Entries: 51

Rep: Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599Reputation: 2599
Not that it applies to you (I think because it doesn't generate reusable history files) but there was one more thing I could think of and that would be Anotatla's Bash logging patch as used in honeypots at the time. Anyway, great to see you got it to work, thanks for posting your solution.
 
  


Reply

Tags
bash, history


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Is there any command to delete bash command history? cola Linux - Newbie 4 06-01-2010 07:32 AM
Bash history delete command from bash itself ashishag Linux - Software 6 05-02-2010 03:39 AM
F8 for command history in bash titanandrews Linux - General 7 10-15-2004 03:25 PM
Need to log users command history FatSteve Linux - Security 2 07-22-2004 06:25 PM
command history log??? granny Linux - Newbie 3 02-11-2003 04:03 PM


All times are GMT -5. The time now is 01:50 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration