Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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 ' 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.