![linux killall](https://i-blog.csdnimg.cn/blog_migrate/a473cc70bc10175ea597cbd25a7fb73f.png)
linux killall
We have previously looked the command kill
to kill process accordion to their names, owners, etc. But using only kill command to kill a process according to its owner is done with supportive commands like grep. In this tutorial, we will look more compact and all in one command killall
.
以前我们已经根据命令的名称,所有者等查看了kill
kill命令来杀死进程,但是仅使用kill命令根据其所有者杀死进程是通过支持命令(例如grep)来完成的。 在本教程中,我们将看起来更紧凑,并且所有命令都可以使用killall
。
killall命令语法 (killall Command Syntax)
The syntax of the killall command is like below.
killall命令的语法如下。
Usage: killall [-Z CONTEXT] [-u USER] [ -eIgiqrvw ] [ -SIGNAL ] NAME...
killall -l, --list
killall -V, --version
![Syntax](https://i-blog.csdnimg.cn/blog_migrate/d809b16087b09604bbeae066f3c106ee.png)
killall命令帮助(killall Command Help)
Simple and fast help information about the command can be listed below. As we see below killall
command provides features like exact process name match, ignoring case, interactive usage, verbose or debug mode operation.
有关该命令的简单和快速帮助信息可以在下面列出。 正如我们在下面看到的, killall
命令提供了一些功能,例如精确的进程名称匹配,忽略大小写,交互式用法,详细或调试模式操作。
$ killall -h
![](https://i-blog.csdnimg.cn/blog_migrate/b9a9ad48228dd98ff24e78a0692b498d.png)
用ps列出进程(List Process With ps)
Killall provides convenient ways to kill a process with its name listing process and using complete process names will make killall operations more reliable. We will list all processes in the system where the user-related process will generally list in the below of the output.
Killall提供了使用其名称列表进程杀死进程的便捷方法,使用完整的进程名称将使killall操作更加可靠。 我们将列出系统中所有与用户相关的进程,这些进程通常会在输出的下面列出。
$ ps uax
![List Process With ps](https://i-blog.csdnimg.cn/blog_migrate/bdaa50371a31632181c923a40c22994b.png)
杀死过程(Kill Process)
Now we can start kill processes according to their name. This example is a simple example where we only provide some term which resides in the process name. Our process is watch ls
like below.
现在我们可以根据它们的名称开始杀死进程。 这个例子是一个简单的例子,其中我们只提供了一些驻留在进程名称中的术语。 我们的过程是watch ls
下方喜欢。
$ ps uax | grep "watch ls"
Now we can provide the term watch
to the killall command like below. And after that command, we can process and see that is killed.
现在,我们可以将术语“ watch
提供给killall命令,如下所示。 在执行该命令之后,我们可以进行处理并看到它被杀死了。
$ killall watch
使用确切的进程名称杀死 (Kill With Exact Process Name)
In the previous example, we have only provided some part of the process name, not the whole. In some situations, this can not be suitable because of similarly named processes. We can specify the exact name to exactly specify the process. In this example, we have two processes with similar names as we can see below.
在前面的示例中,我们仅提供了流程名称的一部分,而不是全部。 在某些情况下,由于名称相似的过程,因此可能不合适。 我们可以指定确切的名称来精确地指定过程。 在此示例中,我们有两个名称相似的进程,如下所示。
$ ps uax | grep "watch ls"
![Kill With Exact Process Name](https://i-blog.csdnimg.cn/blog_migrate/7be43534f4d352fcef394321e79aad58.png)
Now we only want to kill process watch ls
which PID is 3733 . But we do not want to kill watchgnupg
which PID is 3732 . Now we will provide process name with the -e parameter to match the exact name of the process like below.
现在我们只想杀死PID为3733的过程watch ls
。 但是我们不想杀死PID为3732的watchgnupg
。 现在,我们将提供带有-e参数的进程名称,以匹配该进程的确切名称,如下所示。
$ killall -e "watch"
![Kill With Exact Process Name](https://i-blog.csdnimg.cn/blog_migrate/8f77b0b31df32525a1b7d3fc5cd38c6e.png)
We check again the running processes and see only watchgnupg
process.
我们再次检查正在运行的进程,仅查看watchgnupg
进程。
忽略进程名称的区分大小写 (Ignore Casesensitivity For Process Name)
While killing processes the name of the processes is important. The process name can be upper case or lowercase which is not deterministic. In these situations using ignore case parameters is important. -I
parameter can be provided to ignore casesensitivity like below. In the example below, we can see that there is an instance of watch
. We will kill by providing uppercase WATCH
term like below.
在终止进程时,进程的名称很重要。 进程名称可以是大写或小写的,不确定。 在这些情况下,使用忽略大小写参数很重要。 可以提供-I
参数来忽略大小写,如下所示。 在下面的示例中,我们可以看到有一个watch
实例。 我们将通过提供如下所示的大写WATCH
术语来杀死用户。
$ killall -I WATCH
![Ignore Case](https://i-blog.csdnimg.cn/blog_migrate/16acee7bb37bacb90553220910091575.png)
根据进程所有者或用户名杀死(Kill According To Process Owner or User Name)
As we know every Linux process has an owner user name. Killall command supports killing processes according to their user names. Keep in mind that this will kill all processes of the specified user. In these examples we want to kill user name ismail
processes.
众所周知,每个Linux进程都有一个所有者用户名。 Killall命令支持根据用户名杀死进程。 请记住,这将终止指定用户的所有进程。 在这些示例中,我们要ismail
用户名ismail
进程。
$ killall -u ismail
![Kill According To Process User Name](https://i-blog.csdnimg.cn/blog_migrate/e88fed4ddff00f2ad4a7eaa5134640d2.png)
As we see we have killed also our ssh session which the user name was ismail too.
如我们所见,我们也杀死了ssh会话,该会话的用户名也为ismail。
根据进程组名称杀死 (Kill According To Process Group Name)
As we know every Linux process has an owner group name. And this feature is the same as killing according to the user name. We only provide the group name of the processes with the -g
option like below.
众所周知,每个Linux进程都有一个所有者组名称。 并且此功能与根据用户名进行杀死相同。 我们仅使用-g
选项提供进程的组名,如下所示。
$ killall -g ismail
在终止进程之前进行确认 (Confirm Before Killing Processes)
Up to now, we have killed processes harshly without warning of a confirmation. There is an option to ask about confirmation while killing processes like below. The option is -i
. This option is defined also interactively because of its usage way. While killing processes also the PID is provided for stability.
到目前为止,我们已经严厉取消了进程,没有发出确认的警告。 在杀死如下所示的进程时,可以选择询问确认信息。 选项是-i
。 由于其使用方式,因此也以交互方式定义此选项。 在终止进程的同时,还提供了PID以保持稳定性。
$ killall -i watch
根据运行时间终止进程 (Kill Processes According to Running Time)
There is another interesting feature of the killall
command. Processes can be killed according to their run time. In this example, we will kill all processes that run more than 1 minute. This option is -o
killall
命令还有另一个有趣的功能。 可以根据进程的运行时间终止进程。 在此示例中,我们将终止所有运行超过1分钟的进程。 此选项是-o
$ killall -o 1m watch
Also, we can kill processes less than specified time. This is feature is expressed as -y
option.
此外,我们可以在少于指定时间的时间内终止进程。 此功能表示为-y
选项。
$ killall -y 5h watch
We can below how the time is expressed for this feature.
我们可以在下面显示该功能的时间表示方式。
s
secondss
秒m
minutesm
分钟h
hoursh
小时d
daysd
天w
weeksw
周M
monthsM
个月y
yearsy
年
对过程名称使用正则表达式(Using Regular Expression For Process Names)
Up to now, we have used specific processes names. This is simple to express but there are times where we need to expresses generic processes names that are similar but not the same. Regular expressions are the way to express structural parts in the text. In this example, we will kill all processes that start with w and ends with h.
到目前为止,我们已经使用了特定的进程名称。 这很容易表达,但是有时我们需要表达相似但不相同的通用过程名称。 正则表达式是在文本中表达结构部分的方式。 在此示例中,我们将终止所有以w开头和以h结尾的进程。
$ killall -r "w.*h$"
![Using Regular Expression For Process Names](https://i-blog.csdnimg.cn/blog_migrate/c068395a4b067bf6941d8fcc5c98cfc5.png)
In this example only the watch
processes are killed because we have restricted regular expression that the end of the processes name must be h
.
So only watch processes are matched and killed.
在此示例中,仅watch
进程被杀死,因为我们限制了正则表达式,即进程名称的末尾必须为h
。
因此,只有监视进程被匹配并杀死。
翻译自: https://www.poftut.com/killing-linux-processes-killall-command/
linux killall