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.

When I try to install a package, for instance nsnake, with the command sudo apt-get install nsnake &, the process immediately stops. I can bring it to the foreground and enter my sudo password, pause it (with Ctrl-Z), and send it back to the background again. But then the shell will immediately pause the process, e.g.,

katriel@caseylaptop:~$ bg     
[2]+ sudo apt-get install nsnake &   
[2]+  Stopped                 sudo apt-get install nsnake

Is it possible to install packages in the background? I may want to do this while installing large packages on a computer I'm SSH'ing into.

share|improve this question
add comment

4 Answers 4

up vote 24 down vote accepted

Yes, sure.

Perform your apt-get command with fancy things around it

sudo bash -c 'apt-get -y install guake >/dev/null 2>&1 & disown'

Part explanation:

  • The sudo bash -c part spawns a new bash process, and runs apt-get -y install guake >/dev/null 2>&1 & disown inside that new shell.

  • These commands are then run inside the new subshell:

    • apt-get -y install guake: The main apt-get command you want to run.

      • >/dev/null 2>&1 pipes stdout and stderr to /dev/null.
    • & disown disowns the preceding job and exits the subshell.
share|improve this answer
    
Is "yes |" needed when you use the -y switch? –  user31389 Feb 16 at 21:03
    
@user31389: Not strictly, but it may help if a package has further prompts during installation. –  minerz029 Feb 16 at 21:03
    
Would be really nice to understand why this works (and why it is necessary). –  katriel Feb 16 at 21:20
1  
@katriel: It's because Ctrl+Z pauses the command, meaning it is paused (temporarily stopped). –  minerz029 Feb 16 at 21:33
3  
+1 for part explanation. –  Avinash Raj Feb 17 at 2:23
show 10 more comments

I always like to use screen to run programs in the background.

share|improve this answer
2  
+1 for screen. It's easy to use and great for background programs you want to access from time to time, like an irc client or as a substitute for multiple ssh connections when you need to quickly access a set of paths or files repeatedly –  Pichan Feb 17 at 1:59
4  
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. –  blade19899 Feb 17 at 12:16
1  
i prefer tmux to screen. –  Michael Feb 17 at 15:06
add comment

Add the ampersand & to run any program and return to the prompt while the program runs, such as

sudo apt-get -y install nsnake &

You may get certain events and notifications popping up periodically in the shell, however. To hide these events, pipe them to STDOUT

sudo apt-get -y install nsnake >/dev/null &
share|improve this answer
1  
The events were the problem. The solution seems to be to pipe them to STDOUT. –  katriel Feb 17 at 23:39
add comment

It's a workaround, but first run anything with sudo, e.g. sudo ls. Your sudo authorization will be remembered for a while. Then you can just run sudo apt-get -y install nsnake & and you won't be asked for a password second time (unless some time has passed and authorization has expired).

share|improve this answer
    
You still need to type y at the prompt, which your commant wont let you do. –  minerz029 Feb 16 at 20:56
    
@minerz029 Oh, right. Fixed. –  user31389 Feb 16 at 21:02
    
I'm pretty sure neither the sudo authorization nor the "Y/n" prompt that's interfering, since I don't have to do either if I bring the job to the foreground. –  katriel Feb 16 at 21:18
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.