Bash command history keeps getting lost because of a bad connection

My bash command history keeps getting lost because of a bad connection. Microsoft copilot recommended putting this in .bashrc and .bash_profile:

PROMPT_COMMAND='history -a'
shopt -s histappend

Is that the best thing to do? Is there anything better to use?

Those are the two lines I add to my bash config to achieve what you’re after.

It will mix up your history file if you have several terminal sessions running because each command is appended to history as it’s executed. So if you are moving between terminal sessions, it may be harder to determine which commands were executed in which session at a later date.

Without PROMPT_COMMAND=’history -a’ each terminal will write to history when exited so all commands will be grouped together in your history.

In the case of a interrupted and hung login shell (check with who and ps -fe) the suggested history -a is a hack.
Perhaps you can run a tmux session? That keeps your shell running if disconnected, and lets you reconnect to it.

I use mosh when I want to insure connections are not lost / dropped / frozen / etc and if lost or sessions are closed can just login and pick up where it was before.

See

Gemini AI from Google Search results:

Mosh (Mobile Shell) is an alternative to SSH designed to improve the interactive experience of remote terminal sessions, particularly in environments with high latency, intermittent connectivity, or roaming IP addresses. While Mosh uses SSH for initial authentication, it fundamentally differs in how it handles the ongoing session.

Key Features and Differences from SSH:

  • Persistent Sessions:

Mosh sessions remain active even if the client temporarily loses its network connection or changes IP addresses (e.g., switching between Wi-Fi and cellular data). This is a significant advantage over SSH, which typically terminates the session upon network interruption.

  • Roaming:

Mosh allows the client and server to "roam" and change IP addresses without breaking the connection, making it ideal for mobile users or those frequently switching networks.

  • Local Echo and Responsiveness:

Mosh employs a predictive model to provide immediate local echo of keystrokes and line editing, making typing feel more responsive on high-latency connections by reducing the perceived lag.

  • UDP-based Transport:

Unlike SSH, which uses TCP, Mosh utilizes UDP (User Datagram Protocol) for the main session data transfer, enhancing its ability to handle packet loss and network changes. It uses AES-128 OCB mode encryption for secure communication over UDP.

  • SSH for Authentication:

Mosh leverages OpenSSH for the initial authentication process, ensuring a secure and familiar method for establishing the connection.

  • Port Usage:

Mosh typically uses UDP ports in the range of 60,000 to 61,000 for its session data, while still relying on SSH's configured port (defaulting to 22) for the initial authentication.

Neo's Note:

Scrolling back in text sessions do not work well (or at all) with mosh, so if you are working on a task and need to scroll up and down in the terminal window, mosh is not ideal. Otherwise, it's great.