如何在 Linux 系统中结束进程或是中止程序
kill killall

> 
> 在 Linux 中有几种使用命令行或图形界面终止一个程序的方式。
> 
> 
> 


![](/data/attachment/album/201806/04/221410kjzxqzhnwxy5ng6n.jpg)


进程出错的时候，您可能会想要中止或是杀掉这个进程。在本文中，我们将探索在命令行和图形界面中终止进程或是应用程序，这里我们使用 [gedit](https://wiki.gnome.org/Apps/Gedit) 作为样例程序。


### 使用命令行或字符终端界面


#### Ctrl + C


在命令行中调用 `gedit` （如果您没有使用 `gedit &` 命令）程序的一个问题是 shell 会话被阻塞，没法释放命令行提示符。在这种情况下，`Ctrl + C` （`Ctrl` 和 `C` 的组合键） 会很管用。这会终止 `gedit` ，并且所有的工作都将丢失（除非文件已经被保存）。`Ctrl + C` 会给 `gedit` 发送了 `SIGINT` 信号。这是一个默认终止进程的停止信号，它将指示 shell 停止 `gedit` 的运行，并返回到主函数的循环中，您将返回到提示符。



```
$ gedit
^C

```

#### Ctrl + Z


它被称为挂起字符。它会发送 `SIGTSTP` 信号给进程。它也是一个停止信号，但是默认行为不是杀死进程，而是挂起进程。


下面的命令将会停止（杀死/中断） `gedit` 的运行，并返回到 shell 提示符。



```
$ gedit
^Z
[1]+  Stopped                 gedit
$

```

一旦进程被挂起（以 `gedit` 为例），将不能在 `gedit` 中写入或做任何事情。而在后台，该进程变成了一个作业，可以使用 `jobs` 命令验证。



```
$ jobs
[1]+  Stopped                 gedit

```

`jobs` 允许您在单个 shell 会话中控制多个进程。您可以终止，恢复作业，或是根据需要将作业移动到前台或是后台。


让我们在后台恢复 `gedit`，释放提示符以运行其它命令。您可以通过 `bg` 命令来做到，后跟作业 ID（注意上面的 `jobs` 命令显示出来的 `[1]`，这就是作业 ID）。



```
$ bg 1
[1]+ gedit &amp;

```

这和直接使用 `gedit &` 启动程序效果差不多：



```
$ gedit &amp;

```

### 使用 kill


`kill` 命令提供信号的精确控制，允许您通过指定信号名或是信号数字为进程发送信号，后跟进程 ID 或是 PID。


我喜欢 `kill` 命令的一点是它也能够根据作业 ID 控制进程。让我们使用 `gedit &` 命令在后台开启 `gedit` 服务。假设通过 `jobs` 命令我得到了一个 `gedit` 的作业 ID，让我们为 `gedit` 发送 `SIGINT` 信号：



```
$ kill -s SIGINT %1

```

作业 ID 需要使用 `%` 前缀，不然 `kill` 会将其视作 PID。


不明确指定信号，`kill` 仍然可以工作。此时，默认会发送能中断进程的 `SIGTERM` 信号。执行 `kill -l` 可以查看信号名列表，使用 `man kill` 命令阅读手册。


### 使用 killall


如果您不想使用特定的工作 ID 或者 PID，`killall` 允许您使用特定的进程名。中断 `gedit` 最简单的 `killall` 使用方式是：



```
$ killall gedit

```

它将终止所有名为 `gedit` 的进程。和 `kill` 相似，默认发送的信号是 `SIGTERM`。使用 `-I` 选项忽略进程名的大小写。



```
$ gedit &amp;
[1] 14852

$ killall -I GEDIT
[1]+  Terminated              gedit

```

查看手册学习更多 `killall` 命令选项（如 `-u`）。


### 使用 xkill


您是否遇见过播放器崩溃，比如 [VLC](https://www.videolan.org/vlc/index.html) 灰屏或挂起？现在你可以像上面一样获得进程的 PID 来杀掉它，或者使用 `xkill` 命令终止应用程序。


![Using xkill](/data/attachment/album/201806/04/221425pvqtbbhuvpcqhiu4.png "Using xkill")


`xkill` 允许您使用鼠标关闭窗口。仅需在终端执行 `xkill` 命令，它将会改变鼠标光标为一个 **X** 或是一个小骷髅图标。在你想关闭的进程窗口上点击 **x**。小心使用 `xkill`，如手册描述的一致，它很危险。我已经提醒过您了！


参阅手册，了解上述命令更多信息。您还可以接续探索 `pkill` 和 `pgrep` 命令。




---


via: <https://opensource.com/article/18/5/how-kill-process-stop-program-linux>


作者：[Sachin Patil](https://opensource.com/users/psachin) 选题：[lujun9972](https://github.com/lujun9972) 译者：[CYLeft](https://github.com/CYLeft) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
When a process misbehaves, you might sometimes want to terminate or kill it. In this post, we'll explore a few ways to terminate a process or an application from the command line as well as from a graphical interface, using [gedit](https://wiki.gnome.org/Apps/Gedit) as a sample application.

## Using the command line/termination characters

### Ctrl + C

One problem invoking `gedit`

from the command line (if you are not using `gedit &`

) is that it will not free up the prompt, so that shell session is blocked. In such cases, Ctrl+C (the Control key in combination with 'C') comes in handy. That will terminate `gedit`

and all work will be lost (unless the file was saved). Ctrl+C sends the

signal to [SIGINT](http://man7.org/linux/man-pages/man7/signal.7.html)`gedit`

. This is a stop signal whose default action is to terminate the process. It instructs the shell to stop `gedit`

and return to the main loop, and you'll get the prompt back.

```
``````
$ gedit
^C
```


### Ctrl + Z

This is called a *suspend character*. It sends a

signal to process. This is also a stop signal, but the default action is not to kill but to suspend the process.[SIGTSTP](http://man7.org/linux/man-pages/man7/signal.7.html)

It will stop (kill/terminate) `gedit`

and return the shell prompt.

```
``````
$ gedit
^Z
[1]+ Stopped gedit
$
```


Once the process is suspended (in this case, `gedit`

), it is not possible to write or do anything in `gedit`

. In the background, the process becomes a *job*. This can be verified by the `jobs`

command.

```
``````
$ jobs
[1]+ Stopped gedit
```


`jobs`

allows you to control multiple processes within a single shell session. You can stop, resume, and move jobs to the background or foreground as needed.

Let's resume `gedit`

in the background and free up a prompt to run other commands. You can do this using the `bg`

command, followed by job ID (notice `[1]`

from the output of `jobs`

above. `[1]`

is the job ID).

```
``````
$ bg 1
[1]+ gedit &
```


This is similar to starting `gedit`

with `&,`

:

```
```` $ gedit &`


## Using *kill*

`kill`

allows fine control over signals, enabling you to signal a process by specifying either a signal name or a signal number, followed by a process ID, or PID.

What I like about `kill`

is that it can also work with job IDs. Let's start `gedit`

in the background using `gedit &`

. Assuming I have a job ID of `gedit`

from the `jobs`

command, let's send `SIGINT`

to `gedit`

:

```
```` $ kill -s SIGINT %1`


Note that the job ID should be prefixed with `%`

, or `kill`

will consider it a PID.

`kill`

can work without specifying a signal explicitly. In that case, the default action is to send `SIGTERM`

, which will terminate the process. Execute `kill -l`

to list all signal names, and use the `man kill`

command to read the man page.

## Using *killall*

If you don't want to specify a job ID or PID, `killall`

lets you specify a process by name. The simplest way to terminate `gedit`

using `killall`

is:

```
```` $ killall gedit`


This will kill all the processes with the name `gedit`

. Like `kill`

, the default signal is `SIGTERM`

. It has the option to ignore case using `-I`

:

```
``````
$ gedit &
[1] 14852
$ killall -I GEDIT
[1]+ Terminated gedit
```


To learn more about various flags provided by `killall`

(such as `-u`

, which allows you to kill user-owned processes) check the man page (`man killall`

)

## Using *xkill*

Have you ever encountered an issue where a media player, such as [VLC](https://www.videolan.org/vlc/index.html), grayed out or hung? Now you can find the PID and kill the application using one of the commands listed above or use `xkill`

.

`xkill`

allows you to kill a window using a mouse. Simply execute `xkill`

in a terminal, which should change the mouse cursor to an **x** or a tiny skull icon. Click **x** on the window you want to close. Be careful using `xkill`

, though—as its man page explains, it can be dangerous. You have been warned!

Refer to the man page of each command for more information. You can also explore commands like `pkill`

and `pgrep`

.

## 5 Comments