elementary / terminal

Terminal emulator designed for elementary OS
https://elementary.io
GNU Lesser General Public License v3.0
399 stars 96 forks source link

bash history not saved upon reboot #758

Open douglasheld2 opened 3 weeks ago

douglasheld2 commented 3 weeks ago

What Happened?

My Terminal bash history is patchy. I can understand when using multiple tabs, history can get mangled so I've started using a single tab. However, just after reboot I can't find the most recent command I ran and have to Google it again.

Steps to Reproduce

Steps to reproduce:

  1. Open Terminal
  2. type command foo
  3. close Terminal
  4. Open Terminal
  5. type command bar
  6. sudo reboot
  7. log in after reboot
  8. Open Terminal
  9. history

Expected results: output includes "foo" and then "bar"

Actual results: output "foo"

Expected Behavior

When Terminal catches signal from system reboot, it should allow bash shell to save the session history. Terminal should be trapping all necessary signals, including whichever signal is sent from sudo reboot

OS Version

7.x (Horus)

Software Version

Latest release (I have run all updates)

Log Output

N/A (I tried pasting the output from `dmesg` but it was too long for the form. I'm happy to provide further info on request)

Hardware Info

MacbookPro7,1, 16GB RAM bcmwl-kernel-sources driver is installed

jeremypw commented 2 weeks ago

This is because it appears the file ~/.bash_history is not updated until Terminal is closed normally. If you reboot without closing Terminal then the process is just killed and does not perform the normal closing down actions. This applies to a number of apps that do not save their state on the fly.

A quick look at the code indicates this is a feature of the bash shell not of the Terminal app. Terminal only deals with the history of closed tabs (which is not persisted) and the state of open tabs (which is only persisted if the app is closed down normally).

jeremypw commented 2 weeks ago

There is a related issue open here: https://github.com/elementary/gala/issues/537

jeremypw commented 2 weeks ago

I'll look into the possibility of making the shell write that file after each command.

jeremypw commented 2 weeks ago

A possible workaround is detailed here: https://askubuntu.com/questions/67283/is-it-possible-to-make-writing-to-bash-history-immediate

jeremypw commented 2 weeks ago

I can confirm that commands into second and subsequent tabs may not be properly saved to .bash_history probably because they have separate bash processes that store different histories for each tab so there is a possibility of them competing to update the history I guess. Not sure what can be done about that. I guess if the history were updated on the fly it would fix it as it is only possible to enter one command at a time.

Marukesu commented 2 weeks ago

The issue here looks like the same from #616, Terminal doesn't close the shell processes if the system is shutdown with it open.

a possible solution here would be registering with the session and exiting all the child processes so the shell get a chance of writing to the history file.

I can confirm that commands into second and subsequent tabs may not be properly saved to .bash_history probably because they have separate bash processes that store different histories for each tab so there is a possibility of them competing to update the history I guess. Not sure what can be done about that.

last closed tab keeps the history. what could be done by terminal is keep a list of open processes in less recently interacted order just for closing purposes.