How To: Resumable SSH Sessions

in #linux8 years ago (edited)

I


haven't got access to the most reliable internet connections and it is starting to drive me nuts when I have an SSH session open, and the connection goes down, in the middle of a command.

Ok, the regular Witnesses pretty much all would be familiar with using GNU Screen, which allows you to detach sessions without killing the shell (and what was running), but I find the business of switching between tasks using commands extremely disruptive to my mental flow.

So I wondered, is there some way to launch a SSH session with a command that opens a full terminal rather than just running it on the server?

First, you can open a screen session and detach it as normal in an SSH session, of course. But you can instead type this to establish the session and start using it:

ssh inception -t "screen -R"

If you exit this session, it will also close the screen session. But you can either ctrl-a-d out (and straight back to your local terminal) or if the connection dies or you close the terminal tab or window on your machine, so long as you don't type exit in the shell from the one screen launched above, you can type this command and jump straight back

ssh inception -t "screen -R"

Yes, exactly the same command. If a session is open, it will reattach to it. If there is no session, it will make a new one.

Inside the screen session, programs will not be killed when you lose your connection to the server.

The magic is the -t - this creates a terminal connection on the server that you attach to when SSH answers - if you leave it out, the connection will not show you a terminal.

You can use screen on programs that only produce output but don't allow input like this example:

ssh hostname "tail -f steemd.log"

Even though it will stay connected and continue to show you new lines written to the file, when you break it you break your local SSH not the other end, which then breaks because the SSH disconnects. This can be a handy way to check the logs coming out of the remote server without establishing a full session.

To save yourself typing, you can turn the remote-attach ssh command into a shell script, I called mine sshcreen, by:

sudo nano /usr/local/bin/sshcreen

Put these two lines into the file:

#!/bin/bash
ssh $1 -t "screen -R"

Then sudo chmod +x /usr/local/bin/sshcreen to make it executable, and you can now skip the complicated string and type this instead:

sshcreen <server address>
😎


We can't code here! This is Whale country!

Vote #1 l0k1

Go to steemit.com/~witnesses to cast your vote by typing l0k1 into the text entry at the bottom of the leaderboard.

(note, my username is spelled L Zero K One or Lima Zero Kilo One, all lower case)

Sort:  

Thanks, should come in handy.

Thank you for posting @l0k1. Sounds like a plan...it seems that many a site has their issues either frequently or occsasionally...therefore it is good when we have a back-up plan. Thank you.

As far as voting for you as a witness...one hopes it will not make a difference whether one types in ....l0k1...or....10k1....because there is a difference and some have voted for l0k1 and now based on the above instructions some will type in 10k1.

All the best. Cheers.

Steem usernames (as I posted about in a previous post) may only start with a lower case alphabetical letter. Fortunately most people don't misread any of the other characters!

Also, witness usernames can't just be anyone, they have to be users who have broadcast 'intent to be a witness' using a cli_wallet command, so the chances someone votes accidentally for someone else are vanishingly small.

However, I anticipated that so that's why I use code tags around the username in the footer: l0k1, which is much more visually distinct than the variable width normal post font. So also here it is again big:

l0k1

I might make a little subtext in the footer that spells that out.

Thank you for your reply @l0k1. This is helpful to know that the computer does not reject it ....if it is misspelled.

Appreciate you sending it ....BIG.

Apologies for missing the other post.

Regards, bleujay

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 54274.36
ETH 2279.16
USDT 1.00
SBD 2.33