从 Linux 终端终止进程的 3 种办法

注:机翻,未校。


How to Kill Processes From the Linux Terminal

Key Takeaways

  • Killing a process simply means forcing it to quit, and it can be necessary when a process is unresponsive or misbehaving.
    终止进程仅意味着强制其退出,当进程无响应或行为不端时,这可能是必要的。
  • Linux and macOS have commands like kill, pkill, and killall that allow you to terminate processes by either their PID or name.
    Linux 和 macOS 具有 kill、pkill 和 killall 等命令,允许您通过进程的 PID 或名称终止进程。
  • It’s important to be cautious when killing processes and ensure that you are terminating the correct one to avoid any unintended consequences.
    在终止进程时要谨慎,并确保终止正确的进程以避免任何意外后果,这一点很重要。

Killing a process is sometimes the only way to get rid of it. Despite the harsh name, “killing” a process just means “forcing it to quit.” Here’s how to do it from the Linux or macOS command line.
杀死一个进程有时是摆脱它的唯一方法。尽管这个名字很刺耳,但“杀死”一个进程只是意味着“强迫它退出”。以下是从 Linux 或 macOS 命令行执行此操作的方法。

What is a Process?

Running programs like your web browser, background processes associated with your desktop environment, and Linux system services are all processes.
运行程序(如 Web 浏览器)、与桌面环境关联的后台进程以及 Linux 系统服务都是进程。

You can lump processes into two groups:
您可以将流程分为两组:

  • Foreground processes are ones that have been started or launched by a user. They may be in a terminal window, or they may be a graphical application.
    前台进程是由用户启动或启动的进程。它们可能位于终端窗口中,也可能是图形应用程序。
  • Background processes are all of the processes that are started automatically and don’t have any interaction with users. They don’t expect input from users nor do they present results or output to them. Background processes are things like services and daemons.
    后台进程是自动启动的所有进程,与用户没有任何交互。他们不期望用户输入,也不向他们展示结果或输出。后台进程类似于服务和守护进程。

If the foreground processes are the front of theater staff and the actors, the background processes are the backstage “behind the scenes” team.
如果说前台流程是剧院工作人员和演员的前台,那么后台流程就是后台的“幕后”团队。

When processes misbehave or malfunction, they can hog too much CPU time, consume your RAM, or enter a tight computational loop and become unresponsive. Graphical applications can refuse to respond to mouse clicks. Terminal applications might never return you to the command prompt.
当进程行为异常或出现故障时,它们可能会占用过多的 CPU 时间、消耗 RAM 或进入紧密的计算循环并变得无响应。图形应用程序可以拒绝响应鼠标单击。终端应用程序可能永远不会将您返回到命令提示符。

What Does Killing a Process Do?

“Killing” a process just means “forcing the process to quit.” This may be necessary if the process is refusing to respond.
“杀死”一个进程只是意味着“强迫进程退出”。如果进程拒绝响应,则可能需要这样做。

Linux provides the kill, pkill, and killall commands to allow you to do just that. These commands can be used with any type of process, graphical or command line, foreground or background.
Linux 提供了 killpkillkillall 命令来执行此操作。这些命令可以与任何类型的进程、图形或命令行、前台或后台一起使用。

1、The kill Command

To use kill, you must know the process ID (PID) of the process you wish to terminate. The ps command can be used to find the PID of a process.
要使用 kill,您必须知道要终止的进程的进程 ID (PID)。ps 命令可用于查找进程的 PID。

To have ps search through all of the processes use the -e (all processes) option. Piping the output through less is advisable, there’s going to be quite a bit of it. Type ps, a space,-e , a space, | (a pipe character), another space and then type less. Press Enter to execute the command.
要让 ps 搜索所有进程,请使用 -e(所有进程)选项。建议通过管道输出 less,其中会有相当多的输出。键入 ps、空格、-e、空格、|(竖线字符)、另一个空格,然后键入 less。按 Enter 键执行命令。

ps -e | less

ps command in a terminal window

This will give you a process listing that looks similar to the below screenshot. You can search forward in less using the / key and you can search backward using the ? key.
这将为您提供一个类似于以下屏幕截图的流程列表。您可以使用 / 键在 less 中向前搜索,也可以使用 ? 键向后搜索。

output of ps in a less window

To home in on the process you’re interested in, pipe the output from ps through grep and specify the name — or part of the name — of the process.
要了解您感兴趣的流程,请通过管道将输出从 ps 传递到 grep,并指定流程的名称(或名称的一部分)。

ps -e | grep shutter

ps command piped through grep to find the shutter process

Once you have located the PID of the process you wish to terminate, pass it to the kill command as a parameter. To terminate the shutter process identified by the previous command, use this command:
找到要终止的进程的 PID 后,将其作为参数传递给 kill 命令。要终止由上一个命令标识的 shutter 进程,请使用以下命令:

kill 2099

kill command in a terminal window

The kill command is a silent assassin — it does not give you any feedback if it was successful.
kill 命令是一个无声的刺客——如果它成功了,它不会给你任何反馈。

It also works just the same for killing processes on macOS.
它也适用于 macOS 上的终止进程。

2、The pkill Command

The pkill command allows you to kill a process — or processes — by name. You do not need to identify the process by PID. To use pkill you provide a search term that pkill uses to check against the list of running processes. Matching processes are terminated. So you need to be positive you’ve got that search term spelled correctly.
pkill 命令允许您按名称终止一个或多个进程。您不需要通过 PID 识别流程。要使用 pkill,请提供一个搜索词,pkill 用于检查正在运行的进程列表。匹配进程将终止。因此,您需要肯定您已经正确拼写了该搜索词。

As a safety net, you can use the pgrep command before you use the pkill command. The pgrep command also accepts a search term. It will list the PID of each process that matches the search term. This is safe because pgrep will not issue any kill signal to the processes, and if you mistype the search term you will not kill another process by mistake. You can make sure you have the search term correctly thought out before you pass it to pkill. Both pkill and pgrep treat the search term in the same way. Their treatment is so similar that they share the same man page.
作为安全网,您可以在使用 pkill 命令之前使用 pgrep 命令。pgrep 命令也接受搜索词。它将列出与搜索词匹配的每个进程的 PID。这是安全的,因为 pgrep 不会向进程发出任何终止信号,并且如果您输入错误的搜索词,您不会错误地杀死另一个进程。在将搜索词传递给 pkill 之前,您可以确保正确考虑了搜索词。pkillpgrep 都以相同的方式处理搜索词。他们的处理方式非常相似,以至于他们共享同一个手册页。

Let’s suppose there is a process with “subq” in its name. We’ll use the ps -u dave | grep command to get a peek behind the curtain. You can see that “subq” will match that process and that process alone. That was just so you can see the full name of the process.
假设有一个名称中带有“subq”的进程。我们将使用 ps -u dave | grep 命令来窥视幕后。您可以看到“subq”将匹配该进程和该进程。这只是为了让你可以看到进程的全名。

ps -u dave | grep subq

ps command in a terminal window

Let’s assume our user hasn’t done that; all they know is the process name contains the substring “subq.” They use pgrep to check that there is only one match to the search term. They then use that search term with pkill.
假设我们的用户没有这样做;他们只知道进程名称包含子字符串“subq”。他们使用 pgrep 来检查搜索词是否只有一个匹配项。然后,他们将该搜索词与 pkill 一起使用。

pgrep subq
pkill subq

pgrep and pkill in a terminal window

You can use pkill to kill several processes at once. Here the user runs pgrep to check how many processes Chrome has launched. They use pkill to kill them all. They then check with pgrep that they have all been removed.
您可以使用 pkill 一次终止多个进程。在这里,用户运行 pgrep 来检查 Chrome 启动了多少个进程。他们用pkill把他们都杀了。然后,他们与 pgrep 一起检查它们是否已被删除。

pgrep chrome
pkill chrome
pgrep chrome

pgrep and pkill in a terminal window

If several processes with the same name are running, but you do not want to kill them all, you can use pgrep with the -f (command line) option to identify which process is which. A simple example would be two ping processes. You want to kill one of them but not the other. You can use their command lines to distinguish between them. Note the use of quotation marks to wrap the command line parameter.
如果多个同名进程正在运行,但您不想全部终止它们,则可以使用 pgrep-f(命令行)选项来识别哪个进程是哪个进程。一个简单的例子是两个 ping 进程。你想杀死其中一个,但不想杀死另一个。您可以使用它们的命令行来区分它们。请注意,使用引号来换行命令行参数。

pgrep -f "ping 192.168.4.22"
pkill -f "ping 192.168.4.22"

pgrep pkill with ping command line

3、The killall Command

Warning: In the Solaris and OpenIndiana operating systems the killall command will kill all the processes that belong to you. If are root or if you have issued sudo killall you will reboot your computer! During the research for this article, this behavior was confirmed with the latest version of OpenIndiana Hipster 2018.10.
警告:在 Solaris 和 OpenIndiana 操作系统中,killall 命令将终止属于您的所有进程。如果是root或您已发出sudo killall,您将重新启动计算机!在本文的研究过程中,最新版本的 OpenIndiana Hipster 2018.10 证实了此行为。

The killall command operates in a similar way to the pkill command but with a specific difference. Instead of passing a search term to the command you must provide the exact process name.
killall 命令的操作方式与 pkill 命令类似,但有特定的区别。您必须提供确切的进程名称,而不是将搜索词传递给命令。

You cannot provide a partial match to a process name; you must provide the entire process name, as shown:
不能提供与进程名称的部分匹配;您必须提供整个进程名称,如下所示:

killall shutt
killall shutter

killall command in a terminal window

The -y (younger than) option allows you to kill processes that have been running for less than a specified period. The period is given in numbers followed by one of these units:
-y(小于)选项允许您终止运行时间少于指定时间段的进程。周期以数字表示,后跟以下单位之一:

  • s (seconds) s(秒)
  • m (minutes) m(分钟)
  • h (hours) h (小时)
  • d (days) d (天)
  • w (weeks) w(周)
  • M (months, note, capital “M”)
    M(月,注,大写字母“M”)
  • y (years) 和(年)

To kill a process called ana that has just been launched and leave any older instances of ana running, you could use the following parameters with killall, if you’d reacted within two minutes:
要终止刚刚启动的名为 ana 的进程并让 ana 的任何旧实例保持运行状态,如果您在两分钟内做出反应,则可以将以下参数与 killall 一起使用:

killall -y 2m ana

killall with younger than option

The -o (older than) option allows you to kill processes that have been running for longer than a specified period. This command will kill all ssh connections that have been running for longer than a day:
-o(早于)选项允许您终止运行时间超过指定时间段的进程。此命令将终止所有运行时间超过一天的 ssh 连接:

killall -o 1d sshd

killall command in a terminal window

Can You Kill Any Process?

These commands will allow you to identify and terminate errant processes with accuracy and safety correctly. However, you can be too trigger happy, and it is entirely possible to terminate a process that you shouldn’t.
这些命令将使您能够准确、安全地正确识别和终止错误的进程。但是,您可能过于高兴,并且完全有可能终止您不应该终止的过程。

Always be cautious. First, make sure the process you’re about to kill is really the one you want. Second, double check — be careful and ensure the targeted process is the one you want to end. Proceed with terminating the process once you’re satisfied.
始终保持谨慎。首先,确保你要杀死的过程真的是你想要的。其次,仔细检查——要小心,并确保目标过程是你想要结束的过程。满意后继续终止该过程。

If you do terminate a process accidentally, isn’t the end of the world. The most likely outcome is something gets buggy and you have to restart your PC, or you may lose work you’ve done in the program associated with the process you terminated.
如果你不小心终止了一个进程,那不是世界末日吗?最可能的结果是出现问题,您必须重新启动PC,否则可能会丢失在与终止进程关联的程序中完成的工作。


via:

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值