11.2 Foregrounding

If you need to interact with a backgrounded process, you can bring it back into the foreground. If you've only got one backgrounded process, you can bring it back by typing:

% fg

If the program is not done running, the program will take control over you terminal and you will not be returned to a prompt. Sometimes, the program will finish running while backgrounded. In this instance, you'll get a message like this:

[1]+  Done              /bin/ls $LS_OPTIONS

That tells you that the backgrounded process (in this case ls - not terribly interesting) has completed.

It is possible to have several processes backgrounded at once. When this happens, you'll need to know which process you want to bring back to the foreground. Just typing fg will foreground the process that was last backgrounded. What if you had a whole list of processes in the background? Luckily, bash includes a command to list all the processes. It's called jobs and gives output like so:

% jobs
[1]   Stopped                 vim
[2]-  Stopped                 amp
[3]+  Stopped                 man ps

This shows you a list of all the processes that are backgrounded. As you can see, they are all stopped. This means that the processes are suspended. The number is a sort of ID for all the backgrounded processes. The ID with a plus sign beside it (man ps) is the process that will be foregrounded if you just type fg.

If you wanted to foreground vim, you would type:

% fg 1

and vim would spring back up to the console. Backgrounding processes can be very useful if you only have one terminal open over a dialup connection. You can have several programs running on that one terminal, periodically switching back and forth between them.