linux命令详解:pgrep命令

转载:http://www.th7.cn/system/lin/201311/46742.shtml

前言

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

重要选项
    -l 同时显示进程名和PID
    -o 当匹配多个进程时,显示进程号最小的那个
    -n 当匹配多个进程时,显示进程号最大的那个
    注:进程号越大,并不一定意味着进程的启动时间越晚

使用说明
    查看指定名称的进程信息

    默认只显示PID

1
2
3
4
5
       [root@master ~]# pgrep ssh
       3686
       7907
       8815
       12874

    同时显示PID和ProcessName : –l

1
2
3
4
5
       [root@master ~]# pgrep -l sshd
       3686 sshd
       7907 sshd
       8815 sshd
       12874 sshd

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

1
2
3
4
5
6
7
       [root@master ~]# pgrep -l sshd
       3686 sshd
       7907 sshd
       8815 sshd
       12874 sshd
       [root@master ~]# pgrep -l -o  sshd
       3686 sshd

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

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

特别说明

    1)pgrep相当于 ps -eo pid,cmd | awk '{print $1,$2}' | grep KeyWord

1
2
       [root@master ~]# ps -eo pid,cmd | awk '{print $1,$2}'  | grep init
       [root@master ~]# pgrep init

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

    如下,参数里包括要查找的参数,而程序名中不包括,所有没查找到。

1
2
3
4
5
       [root@master ~]# ps axu | grep name
       root     13298  0.0  0.3   5436  1000 pts/4    S    05:52   0:00 sh name.sh
       root     13313  0.0  0.2   4876   672 pts/4    R+   05:53   0:00 grep name
       [root@master ~]# pgrep name
       [root@master ~]#

总结

原文地址:https://www.cnblogs.com/weifeng1463/p/7354956.html