4.1.2.1. Interactive processes
Interactive processes are initialized and controlled through a
terminal session. In other words, there has to be someone connected
to the system to start these processes; they are not started
automatically as part of the system functions. These processes can
run in the foreground, occupying the terminal that started the
program, and you can't start other applications as long as this
process is running in the foreground. Alternatively, they can run
in the background, so that the terminal in which you started the
program can accept new commands while the program is running. Until
now, we mainly focussed on programs running in the foreground - the
length of time taken to run them was too short to notice - but
viewing a file with the less command is a
good example of a command occupying the terminal session. In this
case, the activated program is waiting for you to do something. The
program is still connected to the terminal from where it was
started, and the terminal is only useful for entering commands this
program can understand. Other commands will just result in errors
or unresponsiveness of the system.
While a process runs in the background, however, the user is not
prevented from doing other things in the terminal in which he
started the program, while it is running.
The shell offers a feature called job control which
allows easy handling of multiple processes. This mechanism switches
processes between the foreground and the background. Using this
system, programs can also be started in the background
immediately.
Running a process in the background is only useful for programs
that don't need user input (via the shell). Putting a job in the
background is typically done when execution of a job is expected to
take a long time. In order to free the issuing terminal after
entering the command, a trailing ampersand is added. In the
example, using graphical mode, we open an extra terminal window
from the existing one:
billy:~> xterm &
[1] 26558
billy:~> jobs
[1]+ Running xterm &
|
The full job control features are explained in detail in the
bash Info
pages, so only the frequently used job control applications are
listed here:
Table 4-1. Controlling processes
(part of) command |
Meaning |
regular_command |
Runs this command in the
foreground. |
command
& |
Run this command in the background
(release the terminal) |
jobs |
Show commands running in the
background. |
Ctrl+Z |
Suspend (stop, but not quit) a
process running in the foreground (suspend). |
Ctrl+C |
Interrupt (terminate and quit) a
process running in the foreground. |
%n |
Every process running in the
background gets a number assigned to it. By using the % expression
a job can be referred to using its number, for instance fg %2. |
bg |
Reactivate a suspended program in
the background. |
fg |
Puts the job back in the
foreground. |
kill |
End a process (also see Shell
Builtin Commands in the Info pages of bash) |
More practical examples can be found in the exercises.
Most UNIX systems are likely to be able to run screen, which is useful when you actually want
another shell to execute commands. Upon calling screen, a new session is created with an accompanying
shell and/or commands as specified, which you can then put out of
the way. In this new session you may do whatever it is you want to
do. All programs and operations will run independent of the issuing
shell. You can then detach this session, while the programs you
started in it continue to run, even when you log out of the
originating shell, and pick your screen up again any time
you like.
This program originates from a time when virtual consoles were
not invented yet, and everything needed to be done using one text
terminal. To addicts, it still has meaning in Linux, even though
we've had virtual consoles for almost ten years.