Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
like image 676
openquestion Avatar asked Sep 09 '14 17:09

openquestion


People also ask

How do you find the PID of a specific process?

How to get PID using Task Manager. Press Ctrl+Shift+Esc on the keyboard. Go to the Processes tab. Right-click the header of the table and select PID in the context menu.

How do I show only PID in Linux?

Great answer pgrep -f java . It can be used to get PID only. Your first answer doesn't return only the pid, it returns all informations (on Ubuntu).


2 Answers

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

ps -ef | grep nginx | awk '{print $2}' 9439 
like image 99
Jose Varez Avatar answered Sep 25 '22 05:09

Jose Varez


You can use:

ps -ef | grep '[j]ava' 

Or if pgrep is available then better to use:

pgrep -f java 
like image 39
anubhava Avatar answered Sep 22 '22 05:09

anubhava