Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
19
rated 0 times [  19] [ 0]  / answers: 1 / hits: 105177  / 3 Years ago, mon, november 15, 2021, 8:07:13

It's my understanding that the history file is updated when the terminal exits. But sometimes my computer crashes out, and the terminal doesn't exit cleanly, and then I lose those commands from my history which is annoying. How can I make it flush immediately, so that the entries still go there even if my computer has a meltdown? At the moment I'm using this workaround, but I feel there should be a better way.



I'm using gnome-terminal on Ubuntu 12.10.


More From » bash

 Answers
4

The answers in the link that you provided from the Super-Users site shouldn't necessarily be viewed as 'workarounds' to the history command's default behavior. The bash shell has some sane, out of the box, default behavior.



I would highly recommend reading How can I avoid losing any history lines? for an explanation of what these modifications to history are doing. Additionally, there are some reasonable concerns to be aware of as to why this is not the default behavior of the history command.




  • performance - Since you are saving every command from every window with history -a, the .bash_history file can grow quite large and require greater resources to load the bash shell. This can result in longer start up times(for your terminal sessions, not overall system startup, per se.).


  • organization - (from the above article) "the history commands of simultaneous interactive shells (for a given user) will be intertwined. Therefore the history is not a guaranteed sequential list of commands as they were executed in a single shell."




If you are concerned about further securing the bash shell and the .bash_history file through auditing, take a look at this article: How do I log history or "secure" bash against history removal?



On occasion (e.g. an unstable system, or power failure), I have found the below commands useful.



Add the following lines to your ~/.bashrc file:



unset HISTFILESIZE
HISTSIZE=3000
PROMPT_COMMAND="history -a"
export HISTSIZE PROMPT_COMMAND

shopt -s histappend


Be sure to source your .bashrc file using the command source ~/.bashrc


[#32477] Wednesday, November 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
howesale

Total Points: 224
Total Questions: 117
Total Answers: 116

Location: Nauru
Member since Thu, May 18, 2023
1 Year ago
;