15

I have a virtual instance of redhat running that I can ssh to from any host on the corporate network. My ssh session was dropped while running some scripts that may or not have caused me to disconnect. The who command locally still lists the dropped connection. Reconnecting with ssh starts a whole new session. Is there a way to connect to the original session? If not can I kill it? I have root and physical access. I was not using screen in the original session.

Anthon
  • 79,293
jms
  • 253

3 Answers3

12

You want to use screen on the remote and then when you ssh back in you reconnect to that instance of screen.

but no you can't reconnect to an ssh session in and of itself, you have to use screen (or something else like it to facilitate that).

Look at this question for at least one other option and some differences between it (tmux) and screen. after reading the answer to that question... I'd actually say tmux is better

oh and yes you could kill the process (including the forked bash) to stop it, you might try skill to kill the user by name, but I suspect if that user is root... it might try killing things it can't.

answer has been updated a few times

xenoterracide
  • 59,188
  • 74
  • 187
  • 252
4

By default you cannot re-connect to an abandoned ssh session. However, you can set up processes inside an ssh session, which you can reconnect to after your re-establish a new ssh session.

What you want to use is screen or even better a user-friendly wrapper around screen called byobu.

Screen allows you to run multiple virtual terminal sessions in the same ssh session. A tutorial and help pages are available.

byobu is a wrapper that allows to easily open new screens with a simple function key instead of key combination from ctrl-a. It also shows a status line with all the open virtual terminals which can be named.

Another nice feature is the fact that all your screen can stay up while your ssh connection is disconnected. You just connect again via ssh and call byobu and everything is like before.

At last some screenshots of byobu.

txwikinger
  • 2,226
  • 22
  • 27
3

As mentioned, screen is probably your best bet. If/when your session is dropped, it will keep running. Type 'screen -list' to see available sessions, and 'screen -r <session>' with the session you want. A little tip, you can tab-complete the session string rather than copy/paste the whole thing.

However, screen can be annoying. In my client, you can't easily scroll up to see history in the console. It acts a little differently than you'd expect. If all you want to do is make sure your long running procs don't get killed, use 'nohup'. It will prevent your proc from being shut down when you lose your connection. Make sure you send your proc output to a file if you want to monitor it, though. Also, if you have a bash script, I think you need to explicitly call 'bash' in front of nohup. An example...

nohup bash ./myscript.sh > output.log 2>&1 &

That means nohup, to prevent killing the proc, bash to explicitly call bash if you have bash specific stuff in your script, your script called 'myscript.sh' in the current dir, output.log as the file to send both std out and error out to, and the '&' at the end to run the proc in the background.

Stephen Kitt
  • 434,908
kāgii
  • 374
  • 2
  • 3