You are not logged in.

#1 2012-10-19 12:23:23

csousa
Member
Registered: 2009-05-04
Posts: 32

[SOLVED] Why bash history keeps getting deleted?

Hi,

I want to have a huge bash history, so I set

export HISTSIZE=10000
export HISTFILESIZE=10000

into .bashrc,

It seems to work, however, from times to times (after some reboots, some day, I don't know) the history is trunked to about 500 entries.
Any clue about what's happening?

Last edited by csousa (2012-11-08 18:51:14)

Offline

#2 2012-10-19 12:42:25

bohoomil
Member
Registered: 2010-09-04
Posts: 2,376
Website

Re: [SOLVED] Why bash history keeps getting deleted?

man bash wrote:

      HISTFILESIZE
              The maximum number of lines contained in the history file.  When  this
              variable is assigned a value, the history file is truncated, if neces‐
              sary, by removing the oldest entries, to contain  no  more  than  that
              number  of lines.  The default value is 500.  The history file is also
              truncated to this size after writing  it  when  an  interactive  shell
              exits.
[...]
       HISTSIZE
              The number of commands to remember in the command history (see HISTORY
              below).  The default value is 500.

You may want to try doing the following:

unset HISTFILESIZE
HISTSIZE=10000
#HISTFILESIZE=

:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#3 2012-10-19 12:44:15

DSpider
Member
From: Romania
Registered: 2009-08-23
Posts: 2,273

Re: [SOLVED] Why bash history keeps getting deleted?

Because you can only use one terminal at a time.

If you opened, for example, tilda or guake first, and they're running in the background, using xterm or urxvt won't be able to save to the history file.


"How to Succeed with Linux"

I have made a personal commitment not to reply in topics that start with a lowercase letter. Proper grammar and punctuation is a sign of respect, and if you do not show any, you will NOT receive any help (at least not from me).

Offline

#4 2012-10-19 13:36:43

csousa
Member
Registered: 2009-05-04
Posts: 32

Re: [SOLVED] Why bash history keeps getting deleted?

@bohoomil: I'll try it then, thanks.

@DSpider:
That is not an issue, I use more a than one terminal at a time, and they all append input commands into history at exit time,
I have PROMPT_COMMAND='history -a' in .bashrc.

Offline

#5 2012-10-19 15:49:45

gsgleason
Member
Registered: 2012-10-08
Posts: 59

Re: [SOLVED] Why bash history keeps getting deleted?

Here is what I have in my profile that works well:

export HISTSIZE=10000
export HISTFILESIZE=20000
shopt -s histappend
export PROMPT_COMMAND='history -a'

Read the bash man page about histappend - I think that may be the key.

Last edited by gsgleason (2012-10-19 15:54:59)

Offline

#6 2012-10-19 17:47:53

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 3,497
Website

Re: [SOLVED] Why bash history keeps getting deleted?

gsgleason wrote:
export HISTSIZE=10000
export HISTFILESIZE=20000
shopt -s histappend
export PROMPT_COMMAND='history -a'

I was just updating my bashrc file (thanks to this thread wink) and I'm confused. What's the difference between these two options:

shopt -s histappend
export PROMPT_COMMAND='history -a'

EDIT: I understand "shopt -s histappend" (it's well documented in "man bash"), but "history -a" still confuses me. sad

EDIT: Ok, I think I get it. "shopt -s histappend" means "when I exit this Bash session, append its history to the bash_history file". "export PROMPT_COMMAND='history -a'" means "every time I run a command append it to the bash_history file".

...Wait, doesn't that mean the "history -a" thing makes the "histappend" thing superfluous?

Last edited by drcouzelis (2012-10-19 18:21:24)

Offline

#7 2012-11-08 18:50:23

csousa
Member
Registered: 2009-05-04
Posts: 32

Re: [SOLVED] Why bash history keeps getting deleted?

Ok, after sometime and with a history with 2200 entries, I can say the issue is solved.
The "shopt -s histappend" seems to have solved it.

Thank you all.

Offline

#8 2013-10-23 09:06:08

csousa
Member
Registered: 2009-05-04
Posts: 32

Re: [SOLVED] Why bash history keeps getting deleted?

Argh!

From times to times, history keeps being reset...

I have this in my .bashrc :

unset HISTFILESIZE
export HISTSIZE=10000
export HISTFILESIZE=10000
shopt -s histappend
export PROMPT_COMMAND='history -a'
export HISTCONTROL=ignoredups

Clues, anyone?

Thanks

Last edited by csousa (2013-10-23 09:08:48)

Offline

#9 2013-10-23 11:11:13

vadmium
Member
Registered: 2010-11-02
Posts: 63

Re: [SOLVED] Why bash history keeps getting deleted?

Do you have multiple Bash sessions exiting concurrently? I have run into this problem with losing most of my history when it reaches the size limit (two thousand in my case). I can offer a theory of what could be happening, but no easy solution. And I do not use your PROMPT_COMMAND='history -a' trick, so it might be a slightly different situation.

I did some experimenting with strace and it looked like when the history file has gone over the size limit, it does not update the file it in an atomic manner. It truncated the file first, and only then wrote the new file contents. So if another Bash instance reads the truncated version, or the OS kills Bash or shuts down before the new file contents are written, that would explain the history file getting cleared. In my experience I think I ended up with some of the history from one of the previous sessions, but nothing from another concurrent session, nor previous history.

I’m wondering next time I get too annoyed by this I might try raising Bash’s size limit and implement my own smaller size limit in my .bashrc file, to be enforced when each Bash instance starts up, and/or maybe taking a backup.

When I was investigating I found a relevant Bash mailing list thread from earlier this year, “History file clobbered by multiple simultaneous exits”. It sounds like Bash 4.3 might improve things a bit but there is definitely a race condition in there still.
https://lists.gnu.org/archive/html/bug- … 00031.html

Offline

#10 2013-10-23 11:24:12

csousa
Member
Registered: 2009-05-04
Posts: 32

Re: [SOLVED] Why bash history keeps getting deleted?

Humm...

Indeed it might be that.
Sometimes I have a lot of console tabs opened in one KDE Konsole and then I close the Konsole which closes all consoles simultaneously...

Thanks a lot!

Offline

#11 2014-05-12 11:40:08

Malsasa
Member
From: Indonesia
Registered: 2013-02-13
Posts: 4
Website

Re: [SOLVED] Why bash history keeps getting deleted?

csousa wrote:

Humm...

Indeed it might be that.
Sometimes I have a lot of console tabs opened in one KDE Konsole and then I close the Konsole which closes all consoles simultaneously...

Thanks a lot!

The tricks you have showed above was correct if you only open 1 tab of your Konsole forever. If you open 1 other tab beside 1, then you truncate your history to 500. It is the reason why you got truncated history to 500. We have same problem (I am KDE user too), but I have got good solution:

http://unix.stackexchange.com/questions … al-windows

I copy the selected answer from there:

# avoid duplicates..
export HISTCONTROL=ignoredups:erasedups  
# append history entries..
shopt -s histappend

# After each command, save and reload history
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}history -a; history -c; history -r"

You can try this.

Last edited by Malsasa (2014-05-12 11:40:50)

Offline

#12 2014-05-12 12:53:08

csousa
Member
Registered: 2009-05-04
Posts: 32

Re: [SOLVED] Why bash history keeps getting deleted?

Thanks Malsasa. In fact I'm still getting the history deleted sometimes. I'll try that then.

Offline

#13 2014-09-03 18:41:15

cjmc
Member
Registered: 2014-09-03
Posts: 1

Re: [SOLVED] Why bash history keeps getting deleted?

Do you occasionally log into your box via ssh or tty? Bash only executes ~/.bashrc for non-login shells, like when opening a terminal in a KDE session. You might want to put your HISTSIZE/HISTFILSIZE definitions into ~/.bash_profile instead if that happens to be the reason (see https://www.gnu.org/software/bash/manua … Files.html for an explanation of bash's startup files).

Offline

#14 2014-09-04 15:28:41

csousa
Member
Registered: 2009-05-04
Posts: 32

Re: [SOLVED] Why bash history keeps getting deleted?

cjmc wrote:

Do you occasionally log into your box via ssh or tty?

Indeed I do it from time to time! Thanks for the tip!

Offline

#15 2014-11-25 11:34:00

kozaki
Member
From: London >. < Paris
Registered: 2005-06-13
Posts: 644
Website

Re: [SOLVED] Why bash history keeps getting deleted?

I opened two parallel root bash sessions yesterday, and rebooted after kernel update. Today like csousa found an empty root history.
Am editing ~/.bash_profile as per #11 and now testing.
Thanks you all for sharing.


Seeded week #28: Arch 25 gig (stock 12, Talking 1, Anywhere 11, Apricity 1)
Desktop @3.30GHz 8 gig RAM, Arch linux-ck
laptop1 Atom 2 gig RAM, Arch linux stock i686 (5+H battery smile) & 2: ARM Tegra K1 4 gig RAM, ChrOS
Atom Z520 2 gig RAM, OMV (Debian 7) kernel 3.16 bpo on SDHC | PGP Key: 0xFF0157D9

Offline

#16 2014-11-25 12:58:10

thearcherblog
Member
Registered: 2014-10-30
Posts: 165

Re: [SOLVED] Why bash history keeps getting deleted?

Thanks to this thread I updated my bashrc and bash_profile smile

Thanks a lot smile

Offline

Board footer

Powered by FluxBB