linux pgrep

来源:互联网 发布:sweetalert.js 编辑:程序博客网 时间:2024/05/29 09:24



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&amp;dc=2&amp;di=u848856&amp;dri=0&amp;dis=0&amp;dai=2&amp;ps=230x168&amp;dcb=BAIDU_SSP_define&amp;dtm=HTML_POST&amp;dvi=0.0&amp;dci=-1&amp;dpt=none&amp;tsr=0&amp;tpr=1468740490252&amp;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&amp;ari=2&amp;dbv=2&amp;drs=1&amp;pcs=1217x541&amp;pss=1217x231&amp;cfv=0&amp;cpl=19&amp;chi=1&amp;cce=true&amp;cec=UTF-8&amp;tlm=1468740490&amp;rw=541&amp;ltu=http%3A%2F%2Fwww.th7.cn%2Fsystem%2Flin%2F201311%2F46742.shtml&amp;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&amp;ecd=1&amp;psr=1366x768&amp;par=1366x728&amp;pis=-1x-1&amp;ccd=24&amp;cja=true&amp;cmi=35&amp;col=zh-CN&amp;cdo=-1&amp;tcn=1468740490&amp;qn=75e419c2b5919061&amp;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问题进程

0 0