您的位置:首页 > 运维架构 > Linux

linux pgrep

2016-07-17 15:25 417 查看


linux命令详解:pgrep

前言

经常要查看进程的信息,包括进程的是否已经消亡,通过pgrep来获得正在被调度的进程的相关信息。pgrep通过匹配其程序名,找到匹配的进程

重要选项

-l 同时显示进程名和PID

-o 当匹配多个进程时,显示进程号最小的那个

-n 当匹配多个进程时,显示进程号最大的那个

注:进程号越大,并不一定意味着进程的启动时间越晚

使用说明

查看指定名称的进程信息

默认只显示PID
1: [root@master ~]# pgrep ssh

2: 3686

3: 7907

4: 8815

5: 12874


同时显示PID和ProcessName : –l

1: [root@master ~]# pgrep -l sshd

2: 3686 sshd

3: 7907 sshd

4: 8815 sshd

5: 12874 sshd


-o 当匹配多个进程时,显示进程号最小的那个

1: [root@master ~]# pgrep -l sshd

2: 3686 sshd

3: 7907 sshd

4: 8815 sshd

5: 12874 sshd

6: [root@master ~]# pgrep -l -o  sshd

7: 3686 sshd


-n 当匹配多个进程时,显示进程号最大的那个

1: [root@master ~]# pgrep -l -n sshd

2: 12874 sshd


特别说明

1)pgrep相当于 ps –eo pid,cmd | awk ‘{print $1,$2}’ | grep KeyWord
1: [root@master ~]# ps -eo pid,cmd | awk '{print $1,$2}'  | grep init

2: 1 init

3: [root@master ~]# pgrep init

4: 1


2)如1),pgrep查找的是程序名,不包括其参数

如下,参数里包括要查找的参数,而程序名中不包括,所有没查找到。
1: [root@master ~]# ps axu | grep name

2: root     13298  0.0  0.3   5436  1000 pts/4    S    05:52   0:00 sh name.sh

3: root     13313  0.0  0.2   4876   672 pts/4    R+   05:53   0:00 grep name

4: [root@master ~]# pgrep name

5: [root@master ~]#


总结

pgrep命令用来查找进程的信息,通常会和kill命令来连用,在指定条件下kill问题进程


linux命令详解:pgrep命令

2013-11-05 08:15:27cnblogs.com-李文刚-点击数:11344



<iframe id="iframeu848856_0" src="http://pos.baidu.com/hcdm?rdid=848856&dc=2&di=u848856&dri=0&dis=0&dai=2&ps=230x168&dcb=BAIDU_SSP_define&dtm=HTML_POST&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1468740490252&ti=linux%E5%91%BD%E4%BB%A4%E8%AF%A6%E8%A7%A3%EF%BC%9Apgrep%E5%91%BD%E4%BB%A4-Linux-%E7%AC%AC%E4%B8%83%E5%9F%8E%E5%B8%82&ari=2&dbv=2&drs=1&pcs=1217x541&pss=1217x231&cfv=0&cpl=19&chi=1&cce=true&cec=UTF-8&tlm=1468740490&rw=541&ltu=http%3A%2F%2Fwww.th7.cn%2Fsystem%2Flin%2F201311%2F46742.shtml&ltr=http%3A%2F%2Fwww.so.com%2Flink%3Furl%3Dhttp%253A%252F%252Fwww.th7.cn%252Fsystem%252Flin%252F201311%252F46742.shtml%26q%3Dlinux%2Bpgrep%26ts%3D1468739677%26t%3D2d1759ec6c7a8340a76b080ee70d201%26src%3Dhaosou&ecd=1&psr=1366x768&par=1366x728&pis=-1x-1&ccd=24&cja=true&cmi=35&col=zh-CN&cdo=-1&tcn=1468740490&qn=75e419c2b5919061&tt=1468740490233.136.344.344" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="border-width: 0px; border-style: initial; vertical-align: bottom; margin: 0px;"></iframe>

前言

经常要查看进程的信息,包括进程的是否已经消亡,通过pgrep来获得正在被调度的进程的相关信息。pgrep通过匹配其程序名,找到匹配的进程

重要选项

-l 同时显示进程名和PID

-o 当匹配多个进程时,显示进程号最小的那个

-n 当匹配多个进程时,显示进程号最大的那个

注:进程号越大,并不一定意味着进程的启动时间越晚

使用说明

查看指定名称的进程信息

默认只显示PID
1: [root@master ~]# pgrep ssh

2: 3686

3: 7907

4: 8815

5: 12874


同时显示PID和ProcessName : –l

1: [root@master ~]# pgrep -l sshd

2: 3686 sshd

3: 7907 sshd

4: 8815 sshd

5: 12874 sshd


-o 当匹配多个进程时,显示进程号最小的那个

1: [root@master ~]# pgrep -l sshd

2: 3686 sshd

3: 7907 sshd

4: 8815 sshd

5: 12874 sshd

6: [root@master ~]# pgrep -l -o  sshd

7: 3686 sshd


-n 当匹配多个进程时,显示进程号最大的那个

1: [root@master ~]# pgrep -l -n sshd

2: 12874 sshd


特别说明

1)pgrep相当于 ps –eo pid,cmd | awk ‘{print $1,$2}’ | grep KeyWord
1: [root@master ~]# ps -eo pid,cmd | awk '{print $1,$2}'  | grep init

2: 1 init

3: [root@master ~]# pgrep init

4: 1


2)如1),pgrep查找的是程序名,不包括其参数

如下,参数里包括要查找的参数,而程序名中不包括,所有没查找到。
1: [root@master ~]# ps axu | grep name

2: root     13298  0.0  0.3   5436  1000 pts/4    S    05:52   0:00 sh name.sh

3: root     13313  0.0  0.2   4876   672 pts/4    R+   05:53   0:00 grep name

4: [root@master ~]# pgrep name

5: [root@master ~]#


总结

pgrep命令用来查找进程的信息,通常会和kill命令来连用,在指定条件下kill问题进程
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: