Unix and Linux like operating systems processes uses SIGNALS in order to communicate each other. For example If we can to kill a process we need to send a signal to process we want to kill. This signal will be SIGTERM and send by kill
or similar commands. In this tutorial we will examine the SIGTERM signal and compare with SIGKILL signal.
Unix和Linux之类的操作系统进程使用SIGNALS进行通信。 例如,如果我们可以终止进程,则需要向要终止的进程发送信号。 该信号将是SIGTERM,并通过kill
或类似命令发送。 在本教程中,我们将检查SIGTERM信号并与SIGKILL信号进行比较。
Linux Kill Process Tutorial With Examples
信号(SIGTERM)
The SIGTERM signal is a generic signal used to terminate a program. SIGTERM provides an elegance way to kill program. This is useful because SIGTERM can be handled with different ways like block, ignore etc. This is the polite way to kill application or program. The default behavior of the kill command is sending SIGTERM signal to a process.
SIGTERM信号是用于终止程序的通用信号。 SIGTERM提供了一种优雅的方式来杀死程序。 这很有用,因为SIGTERM可以用不同的方式处理,例如块,忽略等。这是杀死应用程序或程序的有礼貌的方式。 kill命令的默认行为是将SIGTERM信号发送到进程。
将SIGTERM信号发送到进程 (Send SIGTERM Signal To A process)
As stated previously we can send SIGTERM signal to a process by simply using kill
command with the Process ID or simply PID. First we need to get the process ID with pgrep command like below. In this example we want to kill Firefox process.
如前所述,我们可以通过简单地使用带有进程ID或PID的kill
命令将SIGTERM信号发送到进程。 首先,我们需要使用pgrep命令获取进程ID,如下所示。 在此示例中,我们要终止Firefox进程。
$ pgrep firefox

AND we will use kill
command default behaivour with the process ID which is 2229 in this case.
并且在这种情况下,我们将使用进程号为2229的kill
命令default behaivour。
$ kill 2229
发送SIGTERM信号根或其他用户进程 (Send SIGTERM Signal Root or Other Users Processes)
In some cases we may need to send SIGTERM signal to the root
or other users processes. We can use sudo
command in other to get root
privileges which will give us power to kill all processes in the Linux system.
在某些情况下,我们可能需要将SIGTERM信号发送到root
或其他用户进程。 我们可以在其他命令中使用sudo
命令获得root
特权,这将使我们有能力杀死Linux系统中的所有进程。
$ sudo kill 2229
SIGTERM与SIGKILL (SIGTERM vs SIGKILL)
SIGTERM and SIGKILL provides similar functionalities. Both kill given process or program. But there are some minor diffirencies.
SIGTERM和SIGKILL提供类似的功能。 两者都杀死给定的进程或程序。 但是有一些细微的差别。
- SIGTERM try to kill politely on the other side SIGKILL will kill in harsh way SIGTERM试图礼貌地杀死对方SIGKILL会严厉地杀死
- SIGTERM can be handled but SIGKILL can not be handled可以处理SIGTERM,但不能处理SIGKILL
- SIGTERM is the first way to kill a process but it do not work SIGKILL can be used. SIGTERM是杀死进程的第一种方法,但是它不起作用。可以使用SIGKILL。
将SIGKILL信号发送到进程 (Send SIGKILL Signal To A Process)
If SIGTERM do not works as we expect we can use the harsh way which is SIGKILL . We can send SIGKILL signal with kill
command with -9
option which specifies SIGKILL signal.
如果SIGTERM无法正常运行,我们可以使用SIGKILL的苛刻方式。 我们可以使用带有-9
选项的kill
命令发送SIGKILL信号,该命令指定SIGKILL信号。
$ kill -9 2220
翻译自: https://www.poftut.com/what-is-linux-sigterm-signal-and-difference-with-sigkill/