linux获取精准进程PID之pgrep命令

pgrep 是通过程序的名字来查询进程的工具,一般是用来判断程序是否正在运行。在服务器的配置和管理中,这个工具常被应用,简单明了。

 

用法:

#pgrep [选项] [程序名]

 

 

  1. 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]

 

 

常用参数

 

-l 列出程序名和进程ID;

-o 进程起始的ID;

-n 进程终止的ID;

 

例如:

[yanue@server ~]$ pgrep -lo nginx
28433 nginx
[yanue@server ~]$ pgrep -l nginx
28433 nginx
28541 nginx
28542 nginx
28543 nginx
28544 nginx
[yanue@server ~]$ pgrep -ln nginx
28544 nginx
[yanue@server ~]$ pgrep nginx
28433
28541
28542
28543
28544

   

另外,还可以使用另外一个ps命令:(点击查看ps命令详解

  1. ps x | grep xxx  | awk '{print $1}'

 

 

实例:

  1. ps x | grep java | awk  '{print $1}'

 

 

注释:

 

1、xxx为执行的命令名称

 

2、举个例子,获取当前用户下的java进程 【pid】

  1. [admin@vm168a ~]$ ps x | grep java | awk ?'{print $1}'
  2. 16920

 

 

3、用到三个命令,ps、grep、awk。

 

要是这样获取不到的话,可以使用ps命令:

    1. ps -ef | grep xxx | grep -v 'grep' | awk '{print $2}'
    2. [yanue@server ~]$ ps -ef | grep nginx | grep -v 'grep' | awk '{print $2}'
    3. 28433
    4. 28541
    5. 28542
    6. 28543
    7. 28544
posted @ 2019-01-20 21:52  Oops!#  阅读(871)  评论(0编辑  收藏  举报