WHEN SSH DISCONNECTS, I GET LOST
Picture the scene. You’re SSHed into a remote server, doing something really important. You’re just in the middle of running a command on the database whenyour Wi-Fi drops.
You connect back to the server immediately, but you start a new
session and you have no idea if the previous operation completed.
Nightmare! A great solution to this is the ‘screen’ tool.
This is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells.
Each virtual terminal has a separate scrollback history buffer and a copy-and-paste mechanism that allows the user to move text regions between windows. When screen is first opened, it creates a single window with a shell in it (or the specified command) and then gets out of your way so that you can use the program as you normally would.
Then, at any time, you can create new (full-screen) windows with other programs in them (including more shells), kill the current window, view a list of the active windows, switch between windows, and more.
All windows run their programs completely independent of each other and, most importantly, programs continue to run when their window is currently not visible and even when the whole screen session is detached from the user’s terminal.
After installing screen, it’s a good idea to just add it to your .profile so it’s started on a new connection. Use screen -RD to reattach to a session else start a new one, and detach a remote session if needed.