第 180 / 187 篇

17.9. 进程控制

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/pskill.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

17.9. Process Control

当你在 shell 提示符下运行一个程序或命令时,shell 通常会创建一个新进程来运行这个程序或命令(进程是操作系统对正在运行的程序的抽象)。有少数shell 内置命令(built-in shell command)由 shell 进程自己执行(例如 cd),但大多数命令都作为独立的进程运行。关于进程的更多信息,参见第 13 章:操作系统。

When you run a program or command at the shell prompt, the shell usually creates a new process that will run the program or command (a process is the operating systems abstraction of a running program). There are a few built-in shell commands that the shell process executes itself (e.g., cd), but most are run as separate processes. See Chapter 13: The Operating System for more information about processes.

Unix shell 支持进程控制(process control)功能,即指定 shell 如何运行一个程序的方式,以及在进程运行时与之交互的方式。

The unix shell supports process control features, which are ways to specify how a program is run by the shell, and ways to interact with the process as it runs.

17.9.1. 在前台和后台运行

17.9.1. Running in the foreground and background

在 shell 提示符下运行一个程序时,它默认在前台运行(runs in the foreground),即程序运行完毕之后,shell 程序才会再次运行并打印出下一个提示符。不过,程序也可以在后台运行(run in the background),即 shell 启动程序运行,但不等它完成就打印出下一个提示符;这让用户能在程序仍在运行时输入另一条命令。要告诉 shell 在后台运行一个程序,用户在命令行的末尾加上 &。例如,下面展示了如何告诉 shell 在后台运行一个 a.out 程序:

When a program is run at the shell prompt, it runs in the foreground by default, meaning that the program runs to completion before the shell program runs again and prints out the next prompt. However, programs can also be run in the background, meaning that the shell starts the program running but does not wait until it completes before printing out the next prompt; this allows a user to enter another command while the program is still running. To tell the shell to run a program in the background, the user adds an & at the end of the command line. For example, here is how to tell the shell to run an a.out program in the background:

1
2
$ ./a.out &
$

ps 命令可以用来列出 shell 中运行的所有程序。例如,这里我们启动两个在后台运行的 a.out 程序,然后输入 ps:

The ps command can be used to list all the programs running in the shell. For example, here we start two a.out programs running in the background, and then type ps:

1
2
3
4
5
6
7
8
$ ./a.out &
$ ./a.out &
$ ps
PID TTY TIME CMD
2729 pts/72 0:03.91 bash
3582 pts/72 0:00.00 ./a.out
3583 pts/72 0:00.00 ./a.out
3584 pts/72 0:00.00 ps

在这个输出示例中,可以看到除了 Bash shell 进程和 ps 进程之外,还有两个 a.out 进程在运行。每个进程都列出了它的 process id(进程 ID),即操作系统为每个进程维护的唯一标识符(例如,bash 进程的 ID 是 2729,运行 a.out 程序的两个进程的 ID 是 3582 和 3583)。

In the example output, you can see two a.out processes are running in addition to the Bash shell process and the ps process. With each process is listed its process id, a unique identifier the operating system keeps for each process (i.e., the bash process id is 2729 and the two processes running the a.out program have process ids 3582 and 3583).

可以用 fg 命令把一个在后台运行的进程移到前台:

A process running in the background can be moved to the foreground using the fg command:

1
2
3
4
5
6
7
8
9
10
11
$ ./a.out &     # run a.out in the background
$ ps
PID TTY TIME CMD
2729 pts/72 0:03.91 bash
3584 pts/72 0:00.00 ./a.out

$ fg # move a.out to the foreground (the shell waits for it to exit)

$ ps # run ps at the shell prompt (a.out has exited)
PID TTY TIME CMD
2729 pts/72 0:03.91 bash

按下 CTRL-Z 可以中断并挂起一个在前台运行的进程,即暂停它的执行(它不再运行)。这会让 shell 再次运行并打印出 shell 提示符。如果用户输入 bg,被挂起的进程会继续运行,但现在是在后台运行。如果用户输入 fg,被挂起的进程会继续在前台运行。下面是一些例子:

By typing CTRL-Z a process running in the foreground can be interrupted and suspended meaning that its execution is paused (it is not running). This causes the shell to run again and print out a shell prompt. If the user types bg, the suspended process will continue running, but now run in the background. If the user types fg the suspended process will continue running in the foreground again. Here are some examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ./a.out     # run a.out in the foreground
# type CTRL-Z to suspend a.out
[1] Stopped ./a.out

$ ps
PID TTY TIME CMD
2729 pts/72 0:03.91 bash
3588 pts/72 0:00.00 ./a.out

$ bg # unsuspend a.out and run in background

$ ps
PID TTY TIME CMD
2729 pts/72 0:03.91 bash
3588 pts/72 0:00.00 ./a.out

$ fg # run backgrounded process in the foreground

17.9.2. 终止正在运行的进程

17.9.2. Killing a running processes

kill 和 pkill 命令用来从 shell 进程向另一个进程发送信号。信号是一个进程通知另一个进程去做某件事的一种方式。每个信号都会触发一种默认行为,退出和忽略是两种常见的默认选项(关于信号的更多信息,参见第 13 章:操作系统)。

The kill and pkill commands are used to send a signal from the shell process to another processes. Signals are a way in which one process can notify another process to do something. Every signal triggers a default behavior, exiting or ignoring are two common default options (see Chapter 13: The Operating System for more information about signals).

kill 命令接收要发送信号的进程的进程 ID(PID),pkill 命令接收要发送信号的命令或可执行文件的名字(pkill 把信号发送给运行 pkill 的用户所拥有的、具有指定名字的所有正在运行的进程)。kill 和 pkill 都接收一个可选的命令行参数,用来指定要发给进程的具体信号。如果不指定具体的信号,两者默认都向指定的进程发送终止信号(SIGTERM)。

The kill command takes the process id (PID) of the process to send the signal, and the pkill command takes the name of the command or executable file to which to send the signal (pkill sends the signal to all running processes with the specified name that are owned by the user running pkill). Both kill and pkill take an optional command line argument that species the specific signal to send to the process. Without specifying the specific signal, both default to sending a termination signal (SIGTERM) to the specified process(s).

这些命令的一个常见用途是向进程发送信号,让它退出(这就是 kill 这个不幸的名字的由来)。注意,在前台运行的进程也可以用 CTRL-C 终止,它向在前台运行的进程发送一个 SIGINT 信号,这个信号默认让进程退出。

A common use of these commands is to send a process a signal to tell it to exit (this is where kill gets its unfortunate name). Note that a process running in the foreground also can be killed by typing CTRL-C, which sends the process running in the foreground a SIGINT signal that by default tells the process to exit.

下面是调用 pkill 终止所有 a.out 进程的例子:

Here is an example call to pkill to kill all a.out processes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ps
PID TTY TIME CMD
3032597 pts/72 00:00:00 bash
3033074 pts/72 00:00:04 a.out
3033076 pts/72 00:00:03 a.out
3033089 pts/72 00:00:02 a.out
3033105 pts/72 00:00:00 ps

$ pkill a.out
[1] Terminated ./a.out
[2]- Terminated ./a.out
[3]+ Terminated ./a.out

$ ps
PID TTY TIME CMD
3032597 pts/72 00:00:00 bash
3033128 pts/72 00:00:00 ps

下面是调用 kill 终止某个特定 a.out 进程的例子,给出的是它的 PID(第一个 ps 列表中的第二个进程):

Here is an example call to kill to kill a specific a.out processes given its PID (the second process in the first ps listing):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ps
PID TTY TIME CMD
3032597 pts/72 00:00:00 bash
3033870 pts/72 00:00:03 a.out
3033883 pts/72 00:00:02 a.out
3033886 pts/72 00:00:01 a.out
3033897 pts/72 00:00:00 ps

$ kill 3033883
[2]- Terminated ./a.out

$ ps
PID TTY TIME CMD
3032597 pts/72 00:00:00 bash
3033870 pts/72 00:00:18 a.out
3033886 pts/72 00:00:16 a.out
3033974 pts/72 00:00:00 ps

注意,kill 只终止了 PID 匹配的那个特定进程,而不是每一个运行同一程序(a.out)的进程。

Note that kill only killed the specific process with a matching pid value and not every process running the same program (a.out).

在这两个例子中,我们都没有指定要发送的信号。在没有任何可选命令行选项的情况下,kill 和 pkill 向进程发送终止信号(SIGTERM,即信号编号 15)。默认情况下,SIGTERM 让收到信号的进程退出。有时你会看到在调用 kill 和 pkill 时带有一个不同的信号编号。在这种情况下,信号编号作为命令行参数指定(-<信号编号>)。例如,我们可以加上信号编号 -9,向进程发送 SIGKILL 信号(又一种让进程退出的方式):

In both of these examples, we are not specifying the signal to send. Without any optional command line options, kill and pkill send the process the terminate signal (SIGTERM or signal number 15). By default, SIGTERM tells the signaled process to exit. Sometimes you will see calls to kill and pkill that include a different signal number. In this case the signal number is specified as a command line argument (-&lt;signal number&gt;'). For example, we could have included the signal number `-9 to send them the SIGKILL signal (yet another way to tell a process to exit):

1
2
$ pkill -9 a.out
$ kill -9 3033883

一般来说,这些命令可以用来向进程发送任何信号。例如,要向进程发送一个 SIGALRM 信号(信号编号为 14),可以这样做:

In general, these commands can be used to send any signal to a process. For example, to send a process a SIGALRM signal (which is signal number 14), you could do this:

1
$ kill -14 3033883

如果进程没有 SIGALRM 信号的信号处理程序,默认行为是这个 SIGALRM 信号被简单地忽略。

If the process doesn’t have a signal handler for the SIGALRM signal, the default behavior is that the SIGALRM signal is just ignored.

译注

这一说法有误。按 signal(7) 手册中的信号列表,SIGALRM(alarm(2) 发出的定时器信号)的默认动作是 Term,即终止进程,而不是忽略。所以如果进程没有为 SIGALRM 注册处理程序,执行 kill -14 <pid> 会让它退出。默认动作为忽略(Ign)的是 SIGCHLD 等少数信号。

signal 的手册页列出了系统中定义的最基本的信号集合(及其信号编号):man 7 signal。

The man page for signal lists the minimal set of signals (and their signal numbers) that are defined in the system: man 7 signal.

17.9.3. 参考资料

17.9.3. References

更多信息参见:

For more information see:

  • 这些命令的手册页(例如 man ps、man pkill、man 7 signal)
  • The man pages for these commands (e.g., man ps, man pkill, man 7 signal)