简体   繁体   中英

How to get pid by unique process name in linux?

I have two java program running on server MyProgram and MyProgramTest .

ps -ef | grep -i java

root       505 17711  0 16:54 pts/4    00:00:00 grep -i MyProgram
root     16450 16448 99 16:46 pts/4    00:07:29 java MyProgram
root     16473 16471 99 16:46 pts/4    00:07:29 java MyProgramTest

I want to search there pid using below commands

ps ax | grep -v grep | grep MyProgram

It should give me PID 16450 but it is giving both

16450 pts/4    Sl     9:19 java MyProgram
16473 pts/4    Sl     9:19 java MyProgramTest

Expected Output :

16450 pts/4    Sl     9:19 java MyProgram

How to get PID by Unique Process Name in linux ?

ps ax | grep -v grep | grep -w "MyProgram"

要么

ps ax | grep -v grep | grep "\MyProgram\b"

You can use,

ps ax | grep -v grep | grep -w  MyProgram

-w for Whole Word Match. However, It will also match things like MyProgram or MyProgram Hello.

为了避免使用grep -v grep之类的技巧,最好使用pgrep

pgrep --exact MyProgram

Suffix $, ie; the process name ends with "MyProgram",

$ ps -ef |grep MyProgram$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM