Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine history across tty

Tags:

linux

bash

At any given time I have 3 logins to the same server. Sure, I often use screen, but assume this purpose allows me to have:

 1 session running something
 1 session looking at output
 1 session shuffling files to/from various places

If at any point I lose all three connections, I lose 2/3 of my history, as when I log back in, I get the history from a random 1 of my three connections.

Is there a way to stop this and combine all three history files into 1?

Alternatively, is there a way to declare each login as "ttyN" thus keeping each with its separate history separate, but retrievable / re-connectable?

Thanks!

like image 454
Todd Curry Avatar asked Aug 15 '13 17:08

Todd Curry


3 Answers

Possible solution?

After you have opened your Terminal /Screen

start your shell with a History-File setting:

HISTFILE=$HOME/session1-history  bash

and continue to work with this bash. To run a different session history

HISTFILE=$HOME/session2-history  bash

etc.

like image 72
tue Avatar answered Nov 07 '22 02:11

tue


Just add this in your .bashrc file.

# 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"

Found this answer at this Unix Stackexchange post.

I tried this and it seems to work on multiple terminals simultaneously, only catch is you've to execute a command for the terminal to fetch the latest history from .bash_history.

So for example, if I open the first terminal and type echo check 1 and then open a second terminal and type echo check 2. Now if I go back to the first terminal and press the up key, I won't get echo check 2, since the last time the history was fetched from the history file was when I executed the previous command. So I can just press Enter without specifying a command and it will fetch the last history entries. Now if I press the up key, it will show up echo check 2, which is the expected behavior.

like image 45
Chirag Bhatia - chirag64 Avatar answered Nov 07 '22 04:11

Chirag Bhatia - chirag64


There is an open source shell history logger for bash and zsh which would solve your problem (disclaimer: I wrote and maintain it).

https://github.com/barabo/advanced-shell-history

The idea is that your commands are written into a sqlite3 database using a builtin bash hook. If you really needed to go back and figure out what commands you entered / how long they ran / what their exit codes were - you should give it a try.

It's saved me many times.

like image 1
carl.anderson Avatar answered Nov 07 '22 02:11

carl.anderson