学习pgrep,pkill命令

时间:2021-04-02 00:10:23

pgrep, pkill – 查找或者发信号给进程通过名称和其它属性。

语法
pgrep [-flvx] [-n | -o] [-d delim] [-P ppidlist] [-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist] [-G gidlist] [-J projidlist] [-t termlist] [-T taskidlist] [-c ctidlist] [-z zoneidlist] [pattern]

pkill [-signal] [-fvx] [-n | -o] [-P ppidlist] [-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist] [-G gidlist] [-J projidlist] [-t termlist] [-T taskidlist] [-c ctidlist] [-z zoneidlist] [pattern]

说明
pgrep程序检查在系统的中活动进程,报告进程属性匹配命令行上指定条件的进程的ID。每一个进程ID以一个十进制数表示,通过一个分割字符串和下 一个ID分开,默认的分割字符串是一个新行。对于每个属性选项,用户可以在命令行上指定一个以逗号分割的可能值的集合。例如,
pgrep -G other,daemon
匹配真实组ID是other或者是daemon的进程。如果多个条件被指派,这些匹配条件按逻辑与"AND"规则运算。例如,
pgrep -G other,daemon -U root,daemon
匹配进程它的属性是:
(真实的组ID是other或者是daemon) 与 (真实的用户ID是root或者是daemon)
pkill函数和pgrep相同,除了匹配的进程被信号通知就像用kill(1)替代了pgrep的进程ID的输出。一个信号名称或者数字可以作为pkill的第一个命令行选项。

选项
支持下面的选项:The following options are supported:
-c ctidlist  仅匹配列表中列出的ID的进程。
-d delim  指定每一个匹配的进程ID之间分割字符串。如果没有 -d 选项指定,默认的是新行字符。-d 选项仅在pgrep命令中有效。
-f 正则表达式模式将执行与完全进程参数字符串 (从/proc/nnnnn/psinfo文件的pr_psargs字段获得)匹配。如果没有 -f 选项,表达式仅对执行文件名称(从/proc/nnnnn/psinfo文件pr_fname字段获得)匹配。
-g pgrplist 仅匹配进程组ID在给定列表中的进程。如果组0包括在列表中,这个被解释为pgrep或者pkill进程的组ID。
-G gidlist 仅匹配真实组ID在给定列表中的进程。每一个组ID可以使用组名称或者数字的组ID指定。
-J projidlist 匹配项目ID在给定列表中的进程。每一个项目ID可以使用项目的名称或者数字项目ID来指定。
-l 长格式输出。输出每一个匹配进程的名称连同进程ID。进程名称从pr_psargs 或者 pr_fname字段获得,依赖于-f选项是否指定。-l选项仅在pgrep命令中有效。
-n 匹配最新(最近生成的)符合所有其它匹配条件的进程。不能和-o选项一起使用。
-o 匹配最旧(最早生成的)符合所有其它匹配条件的进程。不能和-n选项一起使用。
-P ppidlist 仅匹配给定列表中父进程ID的进程。
-s sidlist 仅匹配进程会话ID在给定列表中的进程。如果ID 0在列表中,这个解释为pgrep或者pikill进程的会话ID。
-t termlist 仅匹配与给定列表中终端关联的进程。每一个终端指定为在/dev中终端设备路径名称的后缀。例如term/a 或者 pts/0。
-T taskidlist 仅匹配在给定列表中任务ID的进程。如果ID 0包括在列表中,这个解释为pgrep或者pikill进程的会话ID。
-u euidlist 仅匹配有效用户ID在给定列表中的进程。每个用户ID可以通过一个登录名称或者数字的用户ID指定。
-U uidlist 仅匹配真实的用户ID在给定列表中的进程。每个用户ID可以通过一个登录名称或者数字的用户ID指定。
-v 反向匹配。匹配所有的进程除了符合匹配条件的。
-x 仅认为进程其参数字符串或者执行文件名称正确匹配规定模式是匹配的进程。模式被认为是准确的当所有在进程参数字符串或者可执行文件名称的字符匹配模式。
-z zoneidlist 仅匹配区域ID在给定列表中的进程。每一个区域ID可以使用一个区域名称或者一个数字的区域ID指定。这个选项仅在全局区域中执行有效。如果pkill程序用来往其它区域的进程发信号,进城必须宣称{PRIV_PROC_ZONE}特权。
-signal 指定发往每一个匹配进程的信号。如果没有指定,SIGTERM 是默认的信号。-signal仅在pkill命令中作为第一个选项有效。信号可以是在signal.h中定义的没有SIG前缀的一个符号名字,也可是一个相应的信号数值。

操作

支持如下操作:
pattern
指定一个扩展正则表达式(Extended Regular Expression (ERE))模式来匹配可执行文件名称或者完整的进程参数字符串。ERE语法的完整描述参看regex(5)。

例子

例子1或者一个进程ID

获得sendmail的进程ID:

example% pgrep -x -u root sendmail
283

例子2终止一个进程

终止最近生成的xterm:

example% pkill -n xterm

Exit Status

如下退出值被返回:

0 一个或者多个进程被匹配。
1 没有进程被匹配。
2 指定了无效的命令行参数。
3 出现一个致命的错误。

NAME

pgrep, pkill – find or signal processes by name and other attributes

Synopsis

pgrep [-flvx] [-n | -o] [-d delim] [-P ppidlist]
[-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist]
[-G gidlist] [-J projidlist] [-t termlist]
[-T taskidlist] [-c ctidlist] [-z zoneidlist]
[pattern]

pkill [-signal] [-fvx] [-n | -o] [-P ppidlist]
[-g pgrplist] [-s sidlist] [-u euidlist] [-U uidlist]
[-G gidlist] [-J projidlist] [-t termlist]
[-T taskidlist] [-c ctidlist] [-z zoneidlist]
[pattern]

Description

The pgrep utility examines the active processes on the system and reports the process IDs of the processes whose attributes match the criteria specified on the command line. Each process ID is printed as a decimal value and is separated from the next ID by a delimiter string, which defaults to a newline. For each attribute option, the user can specify a set of possible values separated by commas on the command line. For example,

pgrep -G other,daemon

matches processes whose real group ID is other OR daemon. If multiple criteria options are specified, pgrep matches processes whose attributes match the logical AND of the criteria options. For example,

pgrep -G other,daemon -U root,daemon

matches processes whose attributes are:

(real group ID is other OR daemon) AND

(real user ID is root OR daemon)

pkill functions identically to pgrep, except that each matching process is signaled as if by kill(1) instead of having its process ID printed. A signal name or number may be specified as the first command line option to pkill.

Options

The following options are supported:

-c ctidlist

Matches only processes whose process contract ID is in the given list.
-d delim

Specifies the output delimiter string to be printed between each matching process ID. If no -d option is specified, the default is a newline character. The -d option is only valid when specified as an option to pgrep.
-f

The regular expression pattern should be matched against the full process argument string (obtained from the pr_psargs field of the /proc/nnnnn/psinfo file). If no -f option is specified, the expression is matched only against the name of the executable file (obtained from the pr_fname field of the /proc/nnnnn/psinfo file).
-g pgrplist

Matches only processes whose process group ID is in the given list. If group 0 is included in the list, this is interpreted as the process group ID of the pgrep or pkill process.
-G gidlist

Matches only processes whose real group ID is in the given list. Each group ID may be specified as either a group name or a numerical group ID.
-J projidlist

Matches only processes whose project ID is in the given list. Each project ID may be specified as either a project name or a numerical project ID.
-l

Long output format. Prints the process name along with the process ID of each matching process. The process name is obtained from the pr_psargs or pr_fname field, depending on whether the -f option was specified (see above). The -l option is only valid when specified as an option to pgrep.
-n

Matches only the newest (most recently created) process that meets all other specified matching criteria. Cannot be used with option -o.
-o

Matches only the oldest (earliest created) process that meets all other specified matching criteria. Cannot be used with option -n.
-P ppidlist

Matches only processes whose parent process ID is in the given list.
-s sidlist

Matches only processes whose process session ID is in in the given list. If ID 0 is included in the list, this is interpreted as the session ID of the pgrep or pkill process.
-t termlist

Matches only processes which are associated with a terminal in the given list. Each terminal is specified as the suffix following “/dev/” of the terminal's device path name in /dev. For example, term/a or pts/0.
-T taskidlist

Matches only processes whose task ID is in the given list. If ID 0 is included in the list, this is interpreted as the task ID of the pgrep or pkill process.
-u euidlist

Matches only processes whose effective user ID is in the given list. Each user ID may be specified as either a login name or a numerical user ID.
-U uidlist

Matches only processes whose real user ID is in the given list. Each user ID may be specified as either a login name or a numerical user ID.
-v

Reverses the sense of the matching. Matches all processes except those which meet the specified matching criteria.
-x

Considers only processes whose argument string or executable file name exactly matches the specified pattern to be matching processes. The pattern match is considered to be exact when all characters in the process argument string or executable file name match the pattern.
-z zoneidlist

Matches only processes whose zone ID is in the given list. Each zone ID may be specified as either a zone name or a numerical zone ID. This option is only useful when executed in the global zone. If the pkill utility is used to send signals to processes in other zones, the process must have asserted the {PRIV_PROC_ZONE} privilege (see privileges(5)).
-signal

Specifies the signal to send to each matched process. If no signal is specified, SIGTERM is sent by default. The value of signal can be one of the symbolic names defined in signal.h(3HEAD) without the SIG prefix, or the corresponding signal number as a decimal value. The -signal option is only valid when specified as the first option to pkill.

Operands

The following operand is supported:

pattern

Specifies an Extended Regular Expression (ERE) pattern to match against either the executable file name or full process argument string. See regex(5) for a complete description of the ERE syntax.

Examples

Example 1 Obtaining a Process ID

Obtain the process ID of sendmail:

example% pgrep -x -u root sendmail
283

Example 2 Terminating a Process

Terminate the most recently created xterm:

example% pkill -n xterm

Exit Status

The following exit values are returned:

0 One or more processes were matched.
1 No processes were matched.
2 Invalid command line options were specified.
3 A fatal error occurred.