Means of saving bash_history regularly - Setup

This is a discussion on Means of saving bash_history regularly - Setup ; When I log on to an ssh session, sometimes the connection breaks due to an activity timeout, and next time I logon the the commands I executed that I expect to find in the bash_history are not there. Is there ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: Means of saving bash_history regularly

  1. Means of saving bash_history regularly


    When I log on to an ssh session, sometimes the connection breaks due to
    an activity timeout, and next time I logon the the commands I executed
    that I expect to find in the bash_history are not there.

    Is there a way to save the bash_history regularly, either via the
    command line, or on some kind of timed basis after logging on?

    I also use the screen command and I would like the history of those
    sessions to be saved separately without getting mixed up.


  2. Re: Means of saving bash_history regularly

    On 2006-10-22, voipfc wrote:
    >
    > When I log on to an ssh session, sometimes the connection breaks due to
    > an activity timeout, and next time I logon the the commands I executed
    > that I expect to find in the bash_history are not there.
    >
    > Is there a way to save the bash_history regularly, either via the
    > command line, or on some kind of timed basis after logging on?


    You can save with:

    history -a [FILENAME]

    I have that in my PROMPT_COMMAND variable.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  3. Re: Means of saving bash_history regularly

    "voipfc" writes:


    >When I log on to an ssh session, sometimes the connection breaks due to
    >an activity timeout, and next time I logon the the commands I executed
    >that I expect to find in the bash_history are not there.


    I have never been able to figure out how the bash history works. I often
    have 5 or 6 windows open. Each of those windows clearly has its own
    history (uparrow brings it down), but most are NOT included in .bash_history. I think it must only
    be the first window opened from the login shell that actually has its
    history saved in .bash_history. The individual shell's histories are saved
    elsewhere.


    >Is there a way to save the bash_history regularly, either via the
    >command line, or on some kind of timed basis after logging on?


    >I also use the screen command and I would like the history of those
    >sessions to be saved separately without getting mixed up.



  4. Re: Means of saving bash_history regularly

    On 2006-10-22, Unruh wrote:
    > "voipfc" writes:
    >
    >
    >>When I log on to an ssh session, sometimes the connection breaks due to
    >>an activity timeout, and next time I logon the the commands I executed
    >>that I expect to find in the bash_history are not there.

    >
    > I have never been able to figure out how the bash history works. I
    > often have 5 or 6 windows open. Each of those windows clearly has
    > its own history (uparrow brings it down), but most are NOT included
    > in .bash_history. I think it must only be the first window opened
    > from the login shell that actually has its history saved in
    > .bash_history. The individual shell's histories are saved elsewhere.


    man bash:

    If the histappend shell option is enabled (see the description
    of shopt under SHELL BUILTIN COMMANDS below), the lines are
    appended to the history file, otherwise the history file is
    overwritten. If HISTFILE is unset, or if the history file is
    unwritable, the history is not saved.

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  5. Re: Means of saving bash_history regularly


    Chris F.A. Johnson wrote:
    > On 2006-10-22, voipfc wrote:
    > >
    > > When I log on to an ssh session, sometimes the connection breaks due to
    > > an activity timeout, and next time I logon the the commands I executed
    > > that I expect to find in the bash_history are not there.
    > >
    > > Is there a way to save the bash_history regularly, either via the
    > > command line, or on some kind of timed basis after logging on?

    >
    > You can save with:
    >
    > history -a [FILENAME]
    >
    > I have that in my PROMPT_COMMAND variable.
    >


    This PROMPT_COMMAND variable - does it work on every separate shell you
    open, and will it automatically chose a different file name?

    > --
    > Chris F.A. Johnson, author |
    > Shell Scripting Recipes: | My code in this post, if any,
    > A Problem-Solution Approach | is released under the
    > 2005, Apress | GNU General Public Licence



  6. Re: Means of saving bash_history regularly

    On 2006-12-01, voipfc wrote:
    >
    > Chris F.A. Johnson wrote:
    >> On 2006-10-22, voipfc wrote:
    >> >
    >> > When I log on to an ssh session, sometimes the connection breaks due to
    >> > an activity timeout, and next time I logon the the commands I executed
    >> > that I expect to find in the bash_history are not there.
    >> >
    >> > Is there a way to save the bash_history regularly, either via the
    >> > command line, or on some kind of timed basis after logging on?

    >>
    >> You can save with:
    >>
    >> history -a [FILENAME]
    >>
    >> I have that in my PROMPT_COMMAND variable.
    >>

    >
    > This PROMPT_COMMAND variable - does it work on every separate shell you
    > open, and will it automatically chose a different file name?


    The contents of PROMPT_COMMAND will be executed if it has been
    set. Do that by putting the assignment in ~/.bashrc or
    ~/.bash_profile (see the bash man page for more info on those
    files).

    You can either supply a file name as part of the command, or you
    can set HISTFILE to a uniqe value for each shell you start, e.g.:

    PROMPT_COMMAND="history -a `date +.bash_history_%Y-%m-%d_%H:%M`"

    HISTFILE=.bash_history_$$_`date +%Y-%m-%d_%H:%M`
    PROMPT_COMMAND="history -a"

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

+ Reply to Thread