说明
本文是个人学习用的非官方中英对照译文,原文: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 ana.outprogram in the background:
1 | $ ./a.out & |
ps 命令可以用来列出 shell 中运行的所有程序。例如,这里我们启动两个在后台运行的 a.out 程序,然后输入 ps:
The
pscommand can be used to list all the programs running in the shell. For example, here we start twoa.outprograms running in the background, and then typeps:
1 | $ ./a.out & |
在这个输出示例中,可以看到除了 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.outprocesses are running in addition to the Bash shell process and thepsprocess. With each process is listed itsprocess id, a unique identifier the operating system keeps for each process (i.e., thebashprocess id is2729and the two processes running thea.outprogram have process ids3582and3583).
可以用 fg 命令把一个在后台运行的进程移到前台:
A process running in the background can be moved to the foreground using the
fgcommand:
1 | $ ./a.out & # run a.out in the background |
按下 CTRL-Z 可以中断并挂起一个在前台运行的进程,即暂停它的执行(它不再运行)。这会让 shell 再次运行并打印出 shell 提示符。如果用户输入 bg,被挂起的进程会继续运行,但现在是在后台运行。如果用户输入 fg,被挂起的进程会继续在前台运行。下面是一些例子:
By typing
CTRL-Za 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 typesbg, the suspended process will continue running, but now run in the background. If the user typesfgthe suspended process will continue running in the foreground again. Here are some examples:
1 | $ ./a.out # run a.out in the foreground |
17.9.2. 终止正在运行的进程
17.9.2. Killing a running processes
kill 和 pkill 命令用来从 shell 进程向另一个进程发送信号。信号是一个进程通知另一个进程去做某件事的一种方式。每个信号都会触发一种默认行为,退出和忽略是两种常见的默认选项(关于信号的更多信息,参见第 13 章:操作系统)。
The
killandpkillcommands 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
killcommand takes the process id (PID) of the process to send the signal, and thepkillcommand takes the name of the command or executable file to which to send the signal (pkillsends the signal to all running processes with the specified name that are owned by the user runningpkill). Bothkillandpkilltake 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
killgets its unfortunate name). Note that a process running in the foreground also can be killed by typingCTRL-C, which sends the process running in the foreground aSIGINTsignal that by default tells the process to exit.
下面是调用 pkill 终止所有 a.out 进程的例子:
Here is an example call to
pkillto kill alla.outprocesses:
1 | $ ps |
下面是调用 kill 终止某个特定 a.out 进程的例子,给出的是它的 PID(第一个 ps 列表中的第二个进程):
Here is an example call to
killto kill a specifica.outprocesses given its PID (the second process in the firstpslisting):
1 | $ ps |
注意,kill 只终止了 PID 匹配的那个特定进程,而不是每一个运行同一程序(a.out)的进程。
Note that
killonly 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,
killandpkillsend the process the terminate signal (SIGTERMor signal number15). By default,SIGTERMtells the signaled process to exit. Sometimes you will see calls tokillandpkillthat include a different signal number. In this case the signal number is specified as a command line argument (-<signal number>'). For example, we could have included the signal number `-9to send them theSIGKILLsignal (yet another way to tell a process to exit):
1 | $ pkill -9 a.out |
一般来说,这些命令可以用来向进程发送任何信号。例如,要向进程发送一个 SIGALRM 信号(信号编号为 14),可以这样做:
In general, these commands can be used to send any signal to a process. For example, to send a process a
SIGALRMsignal (which is signal number14), you could do this:
1 | $ kill -14 3033883 |
如果进程没有 SIGALRM 信号的信号处理程序,默认行为是这个 SIGALRM 信号被简单地忽略。
If the process doesn’t have a signal handler for the
SIGALRMsignal, the default behavior is that theSIGALRMsignal is just ignored.
译注
这一说法有误。按 signal(7) 手册中的信号列表,SIGALRM(alarm(2) 发出的定时器信号)的默认动作是 Term,即终止进程,而不是忽略。所以如果进程没有为 SIGALRM 注册处理程序,执行 kill -14 <pid> 会让它退出。默认动作为忽略(Ign)的是 SIGCHLD 等少数信号。
signal 的手册页列出了系统中定义的最基本的信号集合(及其信号编号):man 7 signal。
The man page for
signallists 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)
- cheat-sheets.org 上的最常用的 Unix 命令
- most used Unix commands from cheat-sheets.org
- gnu.org 上的 Bash 参考手册。
- Bash Reference Manual from gnu.org.