说明
pgrep, pkill - 通过名称或其他属性查找进程ID,并对这些进程ID发送信号量

命令使用

pgrep [-flvx] [-d delimiter] [-n|-o] [-P ppid,...] [-g pgrp,...]
[-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...]
[-t term,...] [pattern]
 
pkill [-signal] [-fvx] [-n|-o] [-P ppid,...] [-g pgrp,...]
[-s sid,...] [-u euid,...] [-U uid,...] [-G gid,...]
[-t term,...] [pattern]

描述

pgrep 可以找出但前运行进程的ID号。通过完全匹配的程序名来获得给定程序名称的进程ID或ID列表,例如:
 
pgrep -u root sshd
 
列出进程名为sshd并且拥有者为root的进程ID。
 
pgrep -u root,daemon
 
列出进程拥有者为root或daemon的进程ID。
 
pkill  将会发送指定的信号量到每一个在标准输出设备中列出的进程ID。信号量的定义通kill命令。

命令选项

-d 分隔符
设置在输出显示时每个进程ID之间的分隔符
(默认为回车换行).  (pgrep 可用.)
 
-f 当我想找一个完成的命令时需要使用给参数。也就是说想查找完整的命令。如下:
pgrep -f "/usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg"
 
-g pgrp,...
列出组ID为给定数字的进程ID。进程组0被解释为pgrep或pkill自己的进程组。
 
-G gid,...
列出用户给出数字的进程ID。可以使用数字或字符
 
-l 列出进程ID的名称。 (pgrep 可用)
 
-n 仅选择最新的进程 (也就是最近启动的) 
 
-o 仅选择最旧的进程 (最早启动的)
 
-P ppid,...
Only match processes whose parent process ID is listed.
 
-s sid,...
Only  match  processes whose process session ID is listed.  Ses-
sion ID 0 is translated into pgrep’s or pkill’s own session  ID.
 
-t term,...
Only  match processes whose controlling terminal is listed.  The
terminal name should be specified without the "/dev/" prefix.
 
-u euid,...
Only match processes whose effective user ID is listed.   Either
the numerical or symbolical value may be used.
 
-U uid,...
Only  match  processes whose real user ID is listed.  Either the
numerical or symbolical value may be used.
 
-v     否定匹配.
 
-x     仅匹配非给定名称的进程。
 
-signal
将信号量发送给每一个匹配的进程。可以使用数字或信号量名称(pkill  可用)

EXAMPLES

Example 1: 查找名为daemon的进程ID:
 
unix$ pgrep -u root named
 
Example 2: 使syslog重新读取配置文件:
 
unix$ pkill -HUP syslogd
 
Example 3: 显示所有xterm进程的详细信息:
 
unix$ ps -fp $(pgrep -d, -x xterm)
 
Example 4: 调节所有netscape进程的的优先级:
 
unix$ renice +4 ‘pgrep netscape‘

退出状态

0      一个或多个进程匹配所给条件。
 
1      没有进程匹配。
 
2      命令行语法错误。
 
3      致命错误: 如内存溢出等.

注意
进程名限制为15个字符。使用-f参数来扩展该限制。

运行pgrep或pkill进程时不会将自己算作匹配项。

附录

代号

名称

内容

1

SIGHUP

启动被终止的程序,可让该 PID 重新读取自己的配置文件,类似重新启动

2

SIGINT

相当于用键盘输入 [ctrl]-c 来中断一个程序的进行

9

SIGKILL

代表强制中断一个程序的进行,如果该程序进行到一半, 那么尚未完成的部分可能会有『半产品』产生,类似 vim会有 .filename.swp 保留下来。

15

SIGTERM

以正常的结束程序来终止该程序。由于是正常的终止, 所以后续的动作会将他完成。不过,如果该程序已经发生问题,就是无法使用正常的方法终止时, 输入这个 signal 也是没有用的。

17

SIGSTOP

相当于用键盘输入 [ctrl]-z 来暂停一个程序的进行