By neildaemond, an 'any-stack' hacking grasshopper.
Taking notes while tinkering with:
Script to auto-reconnect SSH connections to tmux sessions
I like the idea of SSH’ing into a tmux session when doing work on a remote machine so that I can reconnect right where I left off.
However, I don’t like it when the internet connection is unstable, and you must reconnect whenever you drop off. Or, when I close my laptop for a couple of hours and open it back up to a crashed terminal emulator. I tried using mosh
as I’ve done with IRC before, but I didn’t like how the terminal scrolling & copy/paste behaviour was affected unintuitively by mosh & tmux.
Recently, I’ve been doing more work over SSH as I’ve been trying out a rapid prototyping workflow for backend services & APIs. This new development mode was prompted by some exploration on Nix flakes and the Helix IDE (keeping the relevant env and LSP tooling inside the flake definition, so that all dev dependencies are loaded when entering the project folder). I’ll write it up in more detail as soon as possible.
Leveraging some AI prompts, I quickly created the following script with some ease, tweaking it slightly as some ux ripples caught my attention.
When you run this script, it still checks if SSH can connect to the server first.
Then, it will check and list any live tmux sessions. The user can then type in an existing name to reconnect or a new name to start a new session.
If the connection is lost, the user will be allowed 2 seconds to press SPACE key to cancel the reconnect. The 2 seconds will restart if you don’t press SPACE. Otherwise, after 2 seconds, the script will connect back to the same session they were on.
#SSH #Tmux #OnServerDev #CommandLine