Screen is amazing, of course, but I don't want to have to think about it. I often ssh to a machine, start doing a bunch of stuff, and then think "gosh, I wish I had thought to start a screen session before doing all that so I could reconnect to this from home later".
I'd like to have screen automatically started whenever I log in to a machine. And when I get disconnected, I want to be able to immediately and simply reconnect without fussing with "screen -ls" and "screen -dr".
I have a script that implements one solution to this problem which I'll post as an answer. I'm interested to see other approaches.
there is
autossh
which automatically reconnects disconnected ssh-sessions.It comes with an example script called
rscreen
which does exactly that. It is, simply:Then you have to retrain your fingers to type
rscreen hostname
instead ofssh hostname
Maybe put
exec screen -dr
in your .login?This will reload/create your screen session on connect. This does exactly what was requested, even if it moves the responsibility for spawning the session to the initiating client. Ideally you would want some process on the server managing what gets presented to connecting clients. As far as I know that doesn't exist. No one has suggested the ideal solution in this thread. For me this is less "not ideal" than the rest. No scripts, no bugs, no TTY issues, no bad interactions with other ssh commands, no potential for infinite loops, no file editing, no additional packages required.
I have the following in my .bashrc
I found it online somewhere awhile ago, not sure where.
Update Fixed error that was pointed out in comments. Thanks R. Pate
I use mosh (mobile shell). It keeps your connection on even if you go to sleep mode, disconnect from the network, change IP, and so on. Whenever you return, you get your connection back.
Depends on your shell, but what about .bashrc? (If you use bash "screen -rd")