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
Related videos on Youtube
![openquestion](https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/photo.jpg?sz=256)
Author by
openquestion
Updated on July 24, 2020Comments
-
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 almost 8 yearsWhy 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 about 7 yearsMaybe 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 almost 5 yearsClean and quick on Ubuntu 16.04
-
Admin over 4 yearsShorter: ps -ef | grep '[j]ava' | awk '{print $2}'
-
рüффп almost 4 yearsworks as long you don't have multiple instance (e.g. java)
-
рüффп almost 4 yearsI 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 yearsWorks well, hoverver if you use the output as a variable, a | tr -d '\n' must be added at the end of the command.
-
DollyShukla over 3 yearsGreat answer pgrep -f java . It can be used to get PID only.
-
Gilberto Treviño about 3 yearsIt 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 over 2 yearsYour 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 over 2 yearsSquare brackets are used to exclude grep process from ps output
-
Arkham Angel over 2 yearsOn busybox v1.24.1 , this worked for me: ps | grep -v grep | grep cli | awk '{print $1}'
-
Arkham Angel over 2 yearsAnd this: ps | awk '$NF ~ /cli/ {print $1}'
-
Arthur Bowers about 2 yearsThanks for this comment, the ignore grep processes was a nice addition.