How to get only process ID in specify process name

2020-02-17 12:30发布

How to get only the process ID for a specified process name in linux?

ps -ef|grep java    
test 31372 31265  0 13:41 pts/1    00:00:00 grep java

Based on the process id I will write some logic. So how do I get only the process id for a specific process name.

Sample program:

PIDS= ps -ef|grep java
if [ -z "$PIDS" ]; then
echo "nothing"
else
mail test@domain.com
fi

6条回答
forever°为你锁心
2楼-- · 2020-02-17 12:36

You can use:

ps -ef | grep '[j]ava'

Or if pgrep is available then better to use:

pgrep -f java
查看更多
别忘想泡老子
3楼-- · 2020-02-17 12:38
adb shell procrank | grep TYPE_YOUR_PROCESS_NAME_INSTEAD | awk '{print $1}'
查看更多
看我几分像从前
4楼-- · 2020-02-17 12:40

why not just pidof ?

pidof <process_name>

it will return a list of pids matching the process name

https://linux.die.net/man/8/pidof

查看更多
老娘就宠你
5楼-- · 2020-02-17 12:41

This command ignore grep process, and just return PID:

ps -ef | grep -v grep | grep java | awk '{print $2}'
查看更多
对你真心纯属浪费
6楼-- · 2020-02-17 12:43

Use this: ps -C <name> -o pid=

查看更多
淡お忘
7楼-- · 2020-02-17 13:02

You can pipe your output to awk to print just the PID. For example:

ps -ef | grep nginx | awk '{print $2}'
9439
查看更多
登录 后发表回答