Irfy
Irfy

Reputation: 9587

How can I terminate a bash session after ssh-disconnect in order to preserve command history?

When I reconnect to a server after ssh disconnects due to connection issues, I'd like to be able to tell my old bash session to terminate and save its command history.

I'd imagine there would be a way to "write" to the lost shell's tty input buffer, or a signal to send bash.

Upvotes: 3

Views: 470

Answers (1)

Irfy
Irfy

Reputation: 9587

After tinkering with ttyecho which didn't work for me, I realized from the manpage that a clean exit can easily be achieved with a simple SIGHUP signal, not even root is required (if the same user):

$ kill -HUP 11597

will result in "Hangup" being printed in the "lost" shell (in case you test this and have access to it), and history from the lost shell being saved.

An additional:

$ history -a; history -r

would then (1) make sure not to overwrite the history that just got written by our new shell, and (2) make the history from the old shell available to the new shell immediately.

Upvotes: 3

Related Questions