91

How to get only the process ID for a specified process name on 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

5 Answers 5

105

You can use:

ps -ef | grep '[j]ava'

Or if pgrep is available then better to use:

pgrep -f java
3
  • 1
    Great answer pgrep -f java . It can be used to get PID only. Feb 24, 2021 at 10:02
  • 1
    Your first answer doesn't return only the pid, it returns all informations (on Ubuntu). BTW: Could you explain, what the brackets do? II couldn't find a doc for it.
    – dur
    Oct 15, 2021 at 10:55
  • Square brackets are used to exclude grep process from ps output
    – anubhava
    Oct 15, 2021 at 11:07
105

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

ps -ef | grep nginx | awk '{print $2}'
9439
2
  • Works well, hoverver if you use the output as a variable, a | tr -d '\n' must be added at the end of the command.
    – рüффп
    Jul 20, 2020 at 9:46
  • It is a program that can be used to select particular records in a file and perform operations upon them. It is so extensive that you can even write some programs with it. AWK Documentation Jul 1, 2021 at 12:45
80

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

2
  • 18
    Why is this voted down? Not only does it seem to work, but does so using the desired command ps, and no pipe filters. In my case, I couldn't use pipes (reasons..) so this was a lifesaver. You could spend a whole day reading the man page for PS... thanks @ventsyv Jul 29, 2016 at 21:57
  • 2
    Maybe because it's not extremely portable, but then again the other solutions aren't either, and the original question was tagged with Redhat Linux. Just happened to see a commit by one of my engineers who needed to have a portable way to detect a specific java process on OSX, RHEL Linux and AIX, and this is what they came up with: ps -A -o pid,args | grep \[j]ava.
    – ikaerom
    Jun 5, 2017 at 10:51
25

This command ignore grep process, and just return PID:

ps -ef | grep -v grep | grep java | awk '{print $2}'
5
  • 1
    Shorter: ps -ef | grep '[j]ava' | awk '{print $2}'
    – user2849202
    Jan 5, 2020 at 19:48
  • I use this to get the PID. Be careful when using the output as a variable, a | tr -d '\n' must be added at the end.
    – рüффп
    Jul 20, 2020 at 9:45
  • On busybox v1.24.1 , this worked for me: ps | grep -v grep | grep cli | awk '{print $1}' Nov 17, 2021 at 19:07
  • And this: ps | awk '$NF ~ /cli/ {print $1}' Nov 17, 2021 at 19:51
  • 1
    Thanks for this comment, the ignore grep processes was a nice addition. Apr 20, 2022 at 9:30
22

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

2
  • Clean and quick on Ubuntu 16.04 Sep 17, 2019 at 17:30
  • 2
    works as long you don't have multiple instance (e.g. java)
    – рüффп
    Jul 20, 2020 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.