Название | Linux Bible |
---|---|
Автор произведения | Christopher Negus |
Жанр | Зарубежная компьютерная литература |
Серия | |
Издательство | Зарубежная компьютерная литература |
Год выпуска | 0 |
isbn | 9781119578895 |
You can display running processes associated with users other than yourself. To do that, highlight any process in the display (just click it). Then, from the menu button (the button with three bars on it), select All Processes. You can modify processes you don't own only if you are the root user or if you can provide the root password when prompted after you try to change a process. Sometimes, you won't have the luxury of working with a graphical interface. To change processes without a graphical interface, you can use a set of commands and keystrokes to change, pause, or kill running processes. Some of those are described next.
Managing Background and Foreground Processes
If you are using Linux over a network or from a dumb terminal (a monitor that allows only text input with no GUI support), your shell may be all that you have. You may be used to a graphical environment in which you have lots of programs active at the same time so that you can switch among them as needed. This shell thing can seem pretty limited.
Although the bash shell doesn't include a GUI for running many programs at once, it does let you move active programs between the background and foreground. In this way, you can have lots of stuff running and selectively choose the one you want to deal with at the moment.
You can place an active program in the background in several ways. One is to add an ampersand (&
) to the end of a command line when you first run the command. You can also use the at
command to run commands in such a way that they are not connected to the shell.
To stop a running command and put it in the background, press Ctrl+Z. After the command is stopped, you can either bring it back into the foreground to run (the fg
command) or start it running in the background (the bg
command). Keep in mind that any command running in the background might spew output during commands that you run subsequently from that shell. For example, if output appears from a command running in the background during a vi
session, simply press Ctrl+L to redraw the screen to get rid of the output.
TIP
To avoid having the output appear, you should have any process running in the background send its output to a file or to null (add 2> /dev/null
to the end of the command line).
Starting background processes
If you have programs that you want to run while you continue to work in the shell, you can place the programs in the background. To place a program in the background at the time you run the program, type an ampersand (&
) at the end of the command line, like this:
$ find /usr> /tmp/allusrfiles & [3] 15971
This example command finds all files on your Linux system (starting from /usr
), prints those filenames, and puts those names in the file /tmp/allusrfiles
. The ampersand (&
) runs that command line in the background. Notice that the job number, [3]
, and process ID number, 15971
, are displayed when the command is launched. To check which commands you have running in the background, use the jobs
command, as follows:
$ jobs [1] Stopped (tty output) vi /tmp/myfile [2] Running find /usr -print > /tmp/allusrfiles & [3] Running nroff -man /usr/man2/* >/tmp/man2 & [4]- Running nroff -man /usr/man3/* >/tmp/man3 & [5]+ Stopped nroff -man /usr/man4/* >/tmp/man4
The first job shows a text-editing command (vi
) that I placed in the background and stopped by pressing Ctrl+Z while I was editing. Job 2 shows the find
command I just ran. Jobs 3 and 4 show nroff
commands currently running in the background. Job 5 had been running in the shell (foreground) until I decided too many processes were running and pressed Ctrl+Z to stop job 5 until a few processes had completed.
The plus sign (+
) next to number 5 shows that it was most recently placed in the background. The minus sign (-
) next to number 4 shows that it was placed in the background just before the most recent background job. Because job 1 requires terminal input, it cannot run in the background. As a result, it is Stopped
until it is brought to the foreground again.
TIP
To see the process ID for the background job, add a -l
(the lowercase letter L) option to the jobs command. If you type ps
, you can use the process ID to figure out which command is for a particular background job.
Using foreground and background commands
Continuing with the example, you can bring any of the commands on the jobs list to the foreground. For example, to edit myfile
again, enter the following:
$ fg %1
As a result, the vi
command opens again. All text is as it was when you stopped the vi
job.
CAUTION
Before you put a text processor, word processor, or similar program in the background, make sure that you save your file. It's easy to forget that you have a program in the background, and you will lose your data if you log out or the computer reboots.
To refer to a background job (to cancel or bring it to the foreground), use a percent sign (%
) followed by the job number. You can also use the following to refer to a background job:
%
|
Refers to the most recent command put into the background (indicated by the plus sign when you type the jobs command). This action brings the command to the foreground.
|
%string |
Refers to a job where the command begins with a particular string of characters. The string must be unambiguous. (In other words, typing %vi when there are two vi commands in the background results in an error message.)
|
%?string | Refers to a job where the command line contains a string at any point. The string must be unambiguous or the match fails. |
%-- | Refers to the job stopped before the one most recently stopped. |
If a command is stopped, you can start it running again in the background using the bg
command. For example, refer back to job 5 from the jobs list in a previous example:
[5]+ Stopped nroff -man /usr/man4/*>/tmp/man4
Enter the following:
$ bg %5
After that, the job runs in the background. Its jobs
entry appears as follows:
[5] Running nroff -man /usr/man4/*>/tmp/man4 &
Killing and Renicing Processes
Just as you can change the behavior of a process using graphical tools such as System Monitor (described earlier in this chapter), you can also use command-line tools to kill a process or change its CPU priority. The kill
command can send a kill signal to any process to end it, assuming you have permission to do so. It can also send different signals to a process to otherwise change its behavior. The nice
and renice
commands can be used to set or change