How can I run "apt-get install" in the background?

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.


Solution 1:

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.

Solution 2:

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

Solution 3:

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 &