一、命令简介

  Linux pkill 用于杀死一个进程,与 kill 不同的是它会杀死指定名字的所有进程,类似于 killall 命令。kill 命令杀死指定进程 PID,需要配合 ps 使用,而 pkill 直接对进程对名字进行操作,更加方便。

二、使用示例

1、终止一个远程连接

[root@test1 ~]# pkill -kill -t pts/3 #终止pts/3终端的远程连接
在这里插入图片描述

2、终止单个用户的所有进程

[root@test1 ~]# pkill -u es #终止es用户的所有进程
在这里插入图片描述
在这里插入图片描述

3、显示被终止的进程号及数量

使用-e参数显示被终止进程ID信息,-c参数统计被终止的进程数量
[root@test1 ~]# pkill -ecu es
illed (pid 119772)
illed (pid 121704)
illed (pid 121876)
3

4、终端指定pid文件内的进程

使用-F file参数终止file进程文件内的进程
[es@test1 elasticsearch-7.13.2]$ cat pid
121082
[es@test1 elasticsearch-7.13.2]$ pkill -F pid

[1]+ Exit 143 ./bin/elasticsearch -p pid

5、终止匹配命令的时间最早的进程

[es@test1 elasticsearch-7.13.2]$ ps -ef |grep postfix
root 6893 1 0 Jun18 ? 00:00:04 /usr/libexec/postfix/master -w
postfix 6904 6893 0 Jun18 ? 00:00:00 qmgr -l -t unix -u
postfix 115786 6893 0 13:20 ? 00:00:00 pickup -l -t unix -u
es 122281 119772 0 14:39 pts/1 00:00:00 grep --color=auto postfix
[es@test1 elasticsearch-7.13.2]$ pkill -ou postfix
[es@test1 elasticsearch-7.13.2]$ ps -ef |grep postfix
root 6893 1 0 Jun18 ? 00:00:04 /usr/libexec/postfix/master -w
postfix 115786 6893 0 13:20 ? 00:00:00 pickup -l -t unix -u
es 122330 119772 0 14:39 pts/1 00:00:00 grep --color=auto postfix

6、查看命令版本

[root@test1 ~]# pkill -V
pkill from procps-ng 3.3.10

7、获取命令帮助

在这里插入图片描述

三、使用语法及参数说明

1、使用语法

用法:pkill [options]

2、参数说明

参数参数说明
-<sig>, --signal <sig>发送信号(可以是数字或者名字)
-e, --echo显示什么进程被kill了
-c, --count统计匹配的进程数量
-f, --full要求使用完成的进程名字进行匹配
-g, --pgroup <PGID,…>匹配指定的用户组名
-G, --group <GID,…>匹配指定的用户组id
-n, --newestselect most recently started
-o, --oldestselect least recently started
-P, --parent <PPID,…>匹配指定进程的父进程
-s, --session <SID,…>匹配会话id
-t, --terminal <tty,…>匹配控制终端
-u, --euid <ID,…>匹配有效用户
-U, --uid <ID,…>匹配实际用户
-x, --exact根据命令名称匹配扩展内容
-F, --pidfile <file>读取PID文件
-h, --help获取命令帮助
-V, --version查看命令版本
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐