17

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.

4 Answers 4

30

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.
14
  • Is "yes |" needed when you use the -y switch?
    – user31389
    Commented Feb 16, 2014 at 21:03
  • @user31389: Not strictly, but it may help if a package has further prompts during installation.
    – kiri
    Commented Feb 16, 2014 at 21:03
  • Would be really nice to understand why this works (and why it is necessary).
    – katriel
    Commented Feb 16, 2014 at 21:20
  • @katriel: It's not that easy to explain. I've attempted an explanation in my answer, though.
    – kiri
    Commented Feb 16, 2014 at 21:24
  • 1
    @katriel: It's because Ctrl+Z pauses the command, meaning it is paused (temporarily stopped).
    – kiri
    Commented Feb 16, 2014 at 21:33
17

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

3
  • 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
    Commented Feb 17, 2014 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
    Commented Feb 17, 2014 at 12:16
  • 1
    i prefer tmux to screen.
    – Michael
    Commented Feb 17, 2014 at 15:06
9

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 &
1
  • 1
    The events were the problem. The solution seems to be to pipe them to STDOUT.
    – katriel
    Commented Feb 17, 2014 at 23:39
-1

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

3
  • You still need to type y at the prompt, which your commant wont let you do.
    – kiri
    Commented Feb 16, 2014 at 20:56
  • @minerz029 Oh, right. Fixed.
    – user31389
    Commented Feb 16, 2014 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
    Commented Feb 16, 2014 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .