Linux下查看指定进程的PID

pgrep -l name

[root@master ~]# pgrep -l python
45086 python3

 
 
  • 1
  • 2

pgrep的参数:

  • -l 同时显示进程名和PID

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

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

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

查看指定名称的进程信息
默认只显示PID

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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

同时显示PID和ProcessName : –l

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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

-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

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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

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

 
 
  • 1
  • 2

特别说明
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

 
 
  • 1
  • 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 ~]# 

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

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

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e9f16cbbc2.css" rel="stylesheet">
                </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值