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

openquestion picture openquestion · Sep 9, 2014 · Viewed 81.5k times · Source

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 [email protected]
fi

Answer

Jose Varez picture Jose Varez · Sep 9, 2014

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

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