Learning_the_bash_Shell_Third_Edition 16/n
CHAPTER 8
Process Handling
Process IDs and Job Numbers
UNIX gives all processes numbers, called process IDs, when they are created.
Job Control
Foreground and Background
If you have only one background job running, you can use fg without arguments, and the shell will bring that job into the foreground. But if you have several jobs running in the background, the shell will pick the one that you put into the background most recently. If you want some other job put into the foreground, you need to use the job’s command name, preceded by a percent sign (%), or you can use its job number, also preceded by %, or its process ID without a percent sign. If you don’t remember which jobs are running, you can use the command jobs to list them.
jobs has a few interesting options. jobs -l also lists process IDs:
[1] 93 Running alice &
[2]- 102 Running duchess &
[3] 104 Running hatter &
If you type fg without an argument, the shell will put hatter in the foreground,because it was put in the background most recently. But if you type fg %duchess (or fg %2), duchess will go in the foreground.
You can also refer to the job most recently put in the background by % . Similarly, %-refers to the next-most-recently backgrounded job (duchess in this case). That explains the plus and minus signs in the above: the plus sign shows the most recent job whose status has changed; the minus sign shows the next-most-recently invoked job
Suspending a Job
To suspend a job, type CTRL-Z while it is running
You will probably also find it useful to suspend a job and resume it in the background instead of the foreground. You may start a command in the foreground (i.e.,normally) and find that it takes much longer than you expected—for example, a grep, sort, or database query. You need the command to finish, but you would also like control of your terminal back so that you can do other work. If you type CTRL-Z followed by bg, you will move the job to the background.
You can also suspend a job with CTRL-Y. This is slightly different from CTRL-Z in that the process is only stopped when it attempts to read input from the terminal.
Signals
A signal is a message that one process sends to another when some abnormal event takes place or when it wants the other process to do something. Most of the time, a process sends a signal to a subprocess it created. You’re undoubtedly already comfortable with the idea that one process can communicate with another through an I/O pipeline; think of a signal as another way for processes to communicate with each other. (In fact, any textbook on operating systems will tell you that both are examples of the general concept of interprocess communication, or IPC. * )
Depending on the version of UNIX, there are two or three dozen types of signals,including a few that can be used for whatever purpose a programmer wishes. Signals have numbers (from 1 to the number of signals the system supports) and names;we’ll use the latter. You can get a list of all the signals on your system, by name and number, by typing kill -l. Bear in mind, when you write shell code involving signals,that signal names are more portable to other versions of UNIX than signal numbers.