Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've ssh-ed to my ubuntu 15 server box.

And I run apt-get xxx. It takes too long to download, and I don't want to stop it.

How can I turn this job in background just like from the start I run it with "&"

share|improve this question
up vote 8 down vote accepted

You can background a process by typing C-z, at that point, the process is suspended and doesn't do anything other than wait. Then execute bg to allow the process to continue in the background.

jobs will list all the process running/waiting in the background.

testarossa :: ~ % jobs                            
[1]  + suspended  sudo apt-get update

To bring it back to the foreground you can just execute fg. In case you have many processes running in the background you can specify which one to bring to the foreground (or continue in the background) by specifying the process id (e.g., [1] in the above example). So given the above example, bg 1 and bg are equivalent. Note that this is not the pid!

Using a terminal multiplexer like screen and tmux would have prevented this. Might want to check those out.

Example

testarossa :: ~ % sudo apt-get update                                                                                            1 ↵
[sudo] password for christophe: 
Hit http://ppa.launchpad.net trusty InRelease
Hit http://ppa.launchpad.net trusty InRelease                                  
Hit http://ppa.launchpad.net trusty InRelease                                  
Hit http://ppa.launchpad.net trusty InRelease                                  
Hit http://ppa.launchpad.net trusty/main amd64 Packages                        
Hit http://ppa.launchpad.net trusty/main i386 Packages                         
Hit http://ppa.launchpad.net trusty/main Translation-en                        
Ign http://us.archive.ubuntu.com trusty InRelease                              
Hit http://ppa.launchpad.net trusty/main amd64 Packages                        
Hit http://ppa.launchpad.net trusty/main i386 Packages                         
Get:1 https://get.docker.com docker InRelease                                  
100% [Waiting for headers] [Connecting to security.ubuntu.com (91.189.91.14)] [^Z
[1]  + 16963 suspended  sudo apt-get update
testarossa :: ~ % bg                                                                                                            20 ↵
[1]  + 16963 continued  sudo apt-get update

Do mind that output from the process appears on your terminal! You can, however, just do normal stuff on the commandline as you wish.

share|improve this answer
1  
will it keep running in the background ? or just "pause/stop" ? – lovespring Jan 27 at 14:44
5  
If you only execute C-z it will pause. If you execute bg afterwards as well, it will continue. – Christophe De Troyer Jan 27 at 14:46
1  
@ChristopheDeTroyer: I'd would rephrase your answer (even though you already precise the correction) : "You first suspend the foreground job by pressing C-z, [at that point, the process is suspended and doesn't do anything other than wait], and then you allow it to continue running in the background by typing bg. jobs will show you the backgrounded jobs. To bring one in the foreground again (for example, to then be able to stop it), you type: fg %n, n being the job's number (not its pid!). " – Olivier Dulac Jan 27 at 17:25
    
I have extended the answer as per your comment. Thank you! – Christophe De Troyer Jan 27 at 17:32

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.