How to get only process ID in specify process name in linux?

徘徊边缘 提交于 2019-12-18 11:07:28

问题


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

回答1:


You can use:

ps -ef | grep '[j]ava'

Or if pgrep is available then better to use:

pgrep -f java



回答2:


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

ps -ef | grep nginx | awk '{print $2}'
9439



回答3:


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




回答4:


This command ignore grep process, and just return PID:

ps -ef | grep -v grep | grep java | awk '{print $2}'



回答5:


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




回答6:


adb shell procrank | grep TYPE_YOUR_PROCESS_NAME_INSTEAD | awk '{print $1}'


来源:https://stackoverflow.com/questions/25751030/how-to-get-only-process-id-in-specify-process-name-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!