Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

So I was running apt-get upgrade on a server when the router decided it had been too long since it last made me angry: It dropped all connection. Moral of the story is to use screen a lot when you're on a bum router.

Anyway, I logged back in and found in htop that the process was still hanging there, still waiting for my Y/n to upgrade (hadn't hit it yet, luckily). Is there any way I can reattach to a session that had been broken off? I ended up just killing it since it wasn't in the middle of package management but it would be great to know for future reference.

share|improve this question
    
I'm surprised the apt-get process was still running. It should have died along with the whole process chain up to SSH. I've noticed that do-dist-upgrade automatically starts in a screen/byobu session: maybe in some circumstances, apt-get does the same? –  nfirvine Oct 2 '12 at 17:41
add comment

2 Answers

up vote 3 down vote accepted
+50

The answer to your proper question is: you can't. I think the main problem is that the authentication procedures will be out of sync. It Just Doesn't Work Like That.

As you have yourself noticed, the solution is to use screen when possible (by the way, tmux is an alternative to screen).

share|improve this answer
    
Well, thanks for confirming my suspicions. –  user6658 Sep 28 '12 at 16:54
    
But what if you have passwordless ssh? Can you do it then? –  Sridhar-Sarnobat Aug 3 '13 at 17:17
add comment

For running long lasting processes, I use screen, or byobu if you want a more friendly interface.

For screen, you can use:

screen [program] [args]

This will run [program] and its [args] inside a screen session. Once the program is finished, the session is automatically closed. If you wish to keep the session after your program runs, just run screen without any arguments and a new prompt will appear inside the session. CTRL+A+D detaches the terminal from the current session.

To re-attach to a previous session:

screen -r

If there is only one session open, it will reattach immediately. If multiple sessions are ongoing, it will ask you which one you want to attach to. If you know the session name, you can just add it as an argument to this command line.

Byobu is a nice improvement. It's based on screen, but provides a bar at the bottom that shows all current sessions as tabs and gives easier shortcuts to move around those. You can:

  • F2 start a new session
  • F3 move to the next session tab on the left
  • F4 move to the next session tab on the right
  • F8 give a friendly name to the current session tab
  • F9 opens a options menu
  • CTRL+A+D detaches all sessions from the terminal.

WORD OF ADVICE: avoid leaving a session opened with the user root. If anyone gains access to your terminal (locally or remotely), they can easily re-attach to an ongoing session and use your system as root. If needed, it's best to start a session using a common user and sudo indivudual command lines as necessary.

share|improve this answer
1  
May I quote the OP: "Moral of the story is to use screen a lot". Apparently that was not the question here. –  January Sep 28 '12 at 12:13
    
Thanks for the writeup but January was correct. –  user6658 Sep 28 '12 at 16:53
    
Use sudo screen <command> to setup a screen as root, which needs sudo access to reconnect to it. Far better than starting a screen normally, then changing to root within it. –  djsmiley2k Aug 15 '13 at 11:31
add comment

Your Answer

 
discard

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