Home  >  Article  >  System Tutorial  >  How do you kill a process in Linux?

How do you kill a process in Linux?

WBOY
WBOYforward
2024-02-09 19:06:091205browse

If you want to terminate a running Linux process, you can use several methods. If a process is running in the foreground, you can kill it using the shortcut Ctrl C. However, if the process is running in the background and is not visible, you can use a specialized command to kill it.

你是怎么在 Linux 干掉进程的?

To terminate a process, you must stop it during its execution. If you know the ID (PID) of the process, you can kill it using the kill command as follows:

> kill  

In the above syntax, signal refers to the termination signal to be sent, and PID refers to the ID of the process.

There is also a killall command, I will discuss kill and killall commands in this article.

Termination signal in Linux

When a process is terminated by the operating system or user, that is, the process does not complete itself, it will be sent a terminal signal.

The following are the available termination signals:

Signal Value describe
SIGHUP 1 Signal hangup: Sent to the process when the terminal controlling it is closed.
SIGINT 2 Signal interrupt: A signal sent to the process when the user terminates the process. (For example, Ctrl X)
SIGKILL 9 Signal Kill: A signal that immediately exits the process and does not allow it to save the current state.
SIGTERM 15 Signal termination: Send to signal to request termination of the process. This signal can be ignored by the process. But this is the preferred way to terminate a process because it frees resources when the process receives a SIGTERM.
SIGSTOP 19 (for x86, ARM and most others) 17 (for Alpha) 23 (for MIPS) 24 (for PARISC) Stop signal: A signal that stops the process, but will resume later.

常用的信号 9 和 15

获取进程的PID

你还需要了解要终止的进程的详细信息。使用 kill 命令,你必须提供进程的 ID(PID)。你可以从进程名称中获取 PID

> pidof exact_process_name

比如获取java进程的进程号

> pidof java
8075 1032

在 Linux 命令行中终止进程

使用 kill 命令

kill命令要求你知道要终止的进程的 ID,以及可选的终止信号。

要简单地终止命令,请使用以下语法:

kill [signal] 

向 PID 发送终止信号是可选的,如果未提供任何信号,则kill默认发送 SIGTERM ( 15),以正常终止所述进程结束。

我启动了sleep 命令的后台进程(它给了我一个 PID)。我们使用kill命令杀死它。

杀死这个特定的实例sleep如下所示:

$  sleep 120 &
[1] 125686

$ kill 125686
[1]  + terminated  sleep 120

如果我想使用终止信号,我可以使用数值或信号本身:

$ sleep 120 &
[1] 125746

$ kill -SIGKILL 125746
[1]  + killed     sleep 120

$ sleep 120 &
[1] 125759

$ kill -9 125759
[1]  + killed     sleep 120

如果 kill 命令没有指定信号,则默认使用 SIGTERM (15)。

使用 killall 命令

如果不知道某个进程的PID是多少,或者该进程有多个子进程,又想一次性杀死子进程和父进程,可以使用killall命令。

killall [signal] 

kill命令类似,指定终止信号是可选的。当没有指定终止信号时,killall将发送 SIGTERM ( 15) 以优雅地关闭所述进程。

为了演示killall的使用,我将杀死两个正在运行的 sleep 命令。

$ sleep 120 &
[1] 112351

$ sleep 2000 &
[2] 112362

$ killall sleep
[1]-  Terminated              sleep 120
[2]+  Terminated              sleep 2000

此外,你可以使用该-e标志来查找进程名称的完全匹配。

使用 pkill 命令

命令的替代命令killpkill命令。它是pgrepkill命令的组合。

killall杀死所有具有匹配名称的进程。另一方面,pkill使用模式匹配来匹配进程并杀死它们。

pkill [options] pattern

命令中可用的一些有用选项pkill如下:

  • -u: 特定所有者拥有的进程
  • -x: 完全匹配模式的进程
  • -signal: 指定终止信号(默认为 SIGTERM)

结束nginx进程

> pkill nginx

The above is the detailed content of How do you kill a process in Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete