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

101,989

Solution 1

You can use:

ps -ef | grep '[j]ava'

Or if pgrep is available then better to use:

pgrep -f java

Solution 2

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

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

Solution 3

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

Solution 4

This command ignore grep process, and just return PID:

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

Solution 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

Share:
101,989

Related videos on Youtube

openquestion
Author by

openquestion

Updated on July 24, 2020

Comments

  • openquestion
    openquestion almost 4 years

    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
    
  • Scott Prive
    Scott Prive almost 8 years
    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
  • ikaerom
    ikaerom about 7 years
    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.
  • sashaboulouds
    sashaboulouds almost 5 years
    Clean and quick on Ubuntu 16.04
  • Admin
    Admin over 4 years
    Shorter: ps -ef | grep '[j]ava' | awk '{print $2}'
  • рüффп
    рüффп almost 4 years
    works as long you don't have multiple instance (e.g. java)
  • рüффп
    рüффп almost 4 years
    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.
  • рüффп
    рüффп almost 4 years
    Works well, hoverver if you use the output as a variable, a | tr -d '\n' must be added at the end of the command.
  • DollyShukla
    DollyShukla over 3 years
    Great answer pgrep -f java . It can be used to get PID only.
  • Gilberto Treviño
    Gilberto Treviño about 3 years
    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
  • dur
    dur over 2 years
    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.
  • anubhava
    anubhava over 2 years
    Square brackets are used to exclude grep process from ps output
  • Arkham Angel
    Arkham Angel over 2 years
    On busybox v1.24.1 , this worked for me: ps | grep -v grep | grep cli | awk '{print $1}'
  • Arkham Angel
    Arkham Angel over 2 years
    And this: ps | awk '$NF ~ /cli/ {print $1}'
  • Arthur Bowers
    Arthur Bowers about 2 years
    Thanks for this comment, the ignore grep processes was a nice addition.