3. Basic Unix : bg and fg processes

Shifting a process between the foreground and the background

Note

overview: - After starting a

A foreground process is one that occupies your shell (terminal window), meaning that any new commands that are typed have no effect until the previous command is finished. This is as we might expect, but can be confusing when we run long lasting programs, such as the afni or suma GUI (graphical user interface).

Note

afni is no longer appropriate as the example for this, since it now runs in the background by default (it creates a child process and returns the user to an active command prompt). So suma will now be used for this demonstration.

Commands and descriptions:

  • suma : the main AFNI GUI for viewing surface data
  • ctrl-z : keystroke: suspend the foreground process
  • bg : put the recently suspended process in the background
  • fg : put the recently suspended process in the foreground
  • & : run a program in the background to begin with
  • jobs : list child processes under terminal shell

3.1. start suma

Start suma (from any directory). It will display a random dataset (which can be changed with ‘.’).

commands (type these in the terminal window):

suma

When the suma GUI is opened, it shows us something (a random internal dataset of its choosing), which will vary. But this is about Unix, so we do not care what it shows.

To see that suma is active, use the left mouse button (press and hold) to “grab” the surface data and rotate it around.

../_images/basic_2_A.jpg

See also

suma -help

3.2. fail to do anything

Try to type commands in the original terminal window.

Locate the terminal window from which suma was launched. Try to enter some commands into it.

commands (type these in the terminal window):

<Enter>
<Enter>
ls
pwd
<Enter>

We get no prompt and the commands have no effect.

That is because the suma program is running in the foreground of the terminal shell. The shell is occupied by suma.

../_images/basic_2_B.jpg

3.3. suspend suma

In the terminal window, press ctrl-z (hold the ctrl key and press ‘z’).

commands (type these in the terminal window):

ctrl-z
jobs

The terminal window shows ‘Suspended’. That refers to the suma program, meaning that suma is no longer running, but the terminal is available.

The jobs command also shows that suma is suspended.

../_images/basic_2_C.jpg

3.4. try, try, again

Repeat of B: try to type commands in the terminal window.

commands (type these in the terminal window):

<Enter>
<Enter>
ls
pwd
<Enter>

This time the commands work. The suma program is no long occupying the terminal shell.

../_images/basic_2_D.jpg

3.5. is suma broken?

Try to use the suma GUI.

Try to use the left mouse button to grab and rotate the surface again.

Next grab a different window (maybe the terminal or web browser), move it to partially obscure the suma window, and then move it back away.

The windows basically get erased, maybe leaving copies of the obscuring image in the suma window. The suma image is no longer getting re-drawn by the window manager. This shows us that ‘suma‘ is no longer running (since it was suspended).

../_images/basic_2_E.jpg

3.6. wake up, suma!

Put suma into the background, then try to use it again.

Find the same terminal window again and enter the bg command (for background). But watch the suma GUI when you hit <Enter>.

commands (type these in the terminal window):

bg
jobs

Notice that suma started running again, as soon as the bg command was entered. Also, notice that we can still enter commands into the terminal window. So not only is afni running, but we have access to the shell.

Also, notice that jobs shows that suma is running.

../_images/basic_2_F.jpg

3.7. start over

Be efficient. Run ‘suma‘ in the background to begin with. Then ctrl-z and bg would not be needed.

Start by closing the current suma GUI. Use the ‘x’ in the upper corner of the window, or hit <esc> in the suma image window, and click on Yes to close (or just hit <Enter>).

Now re-start suma, putting the & character after the command.

commands (type these in the terminal window):

suma &

Now suma is in the background to begin with. We can use suma and we can type commands in the terminal window.

Note

If a program is running in the background, any text output from the background program will still appear in the same terminal window, which can be confusing.