BASH: Is there a way to automatically save recent lines to my bash history during a period of inactivity?
Asked Answered
C

2

8

The .bash_history file is a life-save for many of us. Unfortunately, BASH only seems to save the commands of a session when that session is closed (via exit).

It's a tragedy, then, when all your commands from an important session are vaporized when a session is closed unexpectedly -- before it gets to archive all the commands with fancy syntax that took hours to get right....

This happens to me when I forget to close a SSH connection when leaving work, and it gets disconnected due to inactivity (Write failed: broken pipe), or when I restart my computer without closing my terminals manually, and so on.

I would love to have my BASH commands archived after some interval -- say every 10 minutes -- so that if I do close a session, my commands will still be there. This seems like something a lot of people might find useful.

Does anyone have an idea of how to do this?

Ideally....

  • The functionality would require no extra effort on the user's part once set up -- something he/she could add to ~/.bashrc
  • The user could change the backup interval
  • It would avoid using temporary files, aliasing bash, or other "hacks"

StackOverflow-ers -- consider yourself challenged!

Curious answered 24/9, 2014 at 23:21 Comment(1)
See unix.stackexchange.com/questions/623700/…Broncobuster
U
13

You can use history command with -a option:

history
-a     Append the ``new'' history lines  (history  lines  entered  since  the
       beginning of the current bash session) to the history file.

You can write each and every command to history file at once with a little help of PROMPT_COMMAND function:

PROMPT_COMMAND
If set, the value is executed as a command  prior  to  issuing  each  primary prompt.

So just put this into .bashrc

PROMPT_COMMAND="history -a"
Unfounded answered 25/9, 2014 at 0:16 Comment(3)
So what this does is archive any new commands entered since the last archival -- effectively each command will be archived upon successful completion of that command, right?Curious
Yes, if by "successful completion" you mean that command was typed-in the command-line and enter was hit. If command had any sense that is another story, but it would be saved in .bash_history.Unfounded
Can this be automated, perhaps by running some cron-like job in the background? If so, I'll mark the question as accepted!Curious
A
4

According to this bash will (usually) receive a SIGHUP on disconnect.

Using trap we can write the history (lame as #*?! that bash doesn't do it by default though..):

function rescue_history {
    history -a
}
trap rescue_history SIGHUP

Put the above in your .bashrc

Allhallowtide answered 20/10, 2016 at 15:7 Comment(2)
Created: unix.stackexchange.com/questions/623700/….Fabozzi
This obviously cannot keep your history in case of system crash or power loss. I'd recommend using PROMPT_COMMAND="history -a; $PROMPT_COMMAND" in your .bashrc instead.Hokusai

© 2022 - 2024 — McMap. All rights reserved.