23

If I start a process by typing it in normally at the command line, such as

wget http://site.com/bigfile.zip

and then decide I want to move that to the background, I know that I can use something like ctrl+z and then bg 1 (or jobs first if needed to get the id) to put it in the background.

My question is if there is a way to move a job from the foreground directly to the background without stopping it - like another shortcut besides ctrl+z that would do that.

I also realize that I can append & to the end of the original command to start it in the background, but the question is in regards to when you start a process in the foreground and then realize you want to move it to the background.

2 Answers 2

14

Yes, absolutely: screen(1) is the answer.

To get started, add screen -R to ~/.bash_profile or equivalent, log out, and log back in.

To continue what's running in the background, press Control-A then N. This will create a new terminal screen in the foreground while seamlessly continuing your running process on the background.

Press Control-A then " to get an interactive list of screens that you have open, or press Control-A then a number to switch directly to a specific screen.

Your original requirement was to be able to move a process to the background without stopping it. Screen has two additional benefits, and these are huge if you like to do multiple things at once:

  1. You can switch between processes much more fluidly than by using Control-Z, bg, fg, etc.
  2. If your terminal session gets disconnected, your processes (like wget http://example.com/bigfile.iso) will NOT terminate! All of your shell sessions will be there waiting for you when you reconnect.

For more information, read the screen(1) manpage, or the GNU Screen Manual.

1
  • 1
    Just make sure you've got the screen package installed first :) Oct 1, 2011 at 17:08
1

bg starts a job that was stopped, and leaves it running in the background. Basically fg and bg do the same, they resume a process, but fg does it in the foreground while bg does it in the background.

Just run bg %(number of the job) and you will have an output saying "runnning". If you check your jobs, you will see that your job is running and not stopped.

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