10

I am trying to get the PID of the process INSIDE adb shell. So, I am doing adb shell which gets me to the android shell. Now, if I were to get the PID using a regular shell I would use

adb shell ps | grep android.process.acore | sed 's/\s\s*/ /g' | cut -d ' ' -f 2

OR

adb shell ps | grep android.process.acore | awk '{ print $2 }'

I get the PID (a numeric number - 2nd field of the ps | grep android.process.acore) output.

However, if I run the above commands inside android shell(after doing adb shell), I get /system/bin/sh: sed: not found and /system/bin/sh: awk: not found errors respectively. Which means, these commands are not available inside adb shell. However, grep works.

The output of the ps | grep android.process.acore inside adb shell is:

XXX_x21   11826 441   502296 39028 ffffffff 4010ff6c S android.process.acore

I am looking for the number 11826. How can I extract it inside adb shell?

Also, please help if there is a direct way to get the PID inside the adb shell.

Regards, Rumit

rumit patel
  • 1,891
  • 5
  • 23
  • 28

5 Answers5

20

Android versions starting with 6.0 already include pidof utility:

usage: pidof [-s] [-o omitpid[,omitpid...]] [NAME]...

Print the PIDs of all processes with the given names.

-s      single shot, only return one pid.
-o      omit PID(s)
Alex P.
  • 30,437
  • 17
  • 118
  • 169
15

Not sure if you can get the PID directly however you can try the following

set `ps |grep android.process.acore`
echo $2

This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2

Steve Weet
  • 28,126
  • 11
  • 70
  • 86
  • Hi Steve, Thank you for your answer. However, I'm stuck using the command inside adb shell via java runexec(). See details here: http://stackoverflow.com/questions/21321085/command-with-pipe-and-spaces-does-not-work-with-runtime-exec any help is appreciated. Regards, – rumit patel Feb 16 '14 at 02:26
0

I tried this one and it seems to work:

adb shell "set "ps | grep android.process.media"; kill -9 $2"
JKennedy
  • 18,150
  • 17
  • 114
  • 198
y0tta
  • 1
0

I tried this one and it seems to work:

  1. adb shell
  2. ps -A | grep "android.process.acore"
General Grievance
  • 4,555
  • 31
  • 31
  • 45
BASAVARAJ PATIL
  • 436
  • 3
  • 9
0
adb shell pidof [package name]

or

adb shell pidof -s [package name]

-s option is for single shot, returning only one pid.

oiyio
  • 5,219
  • 4
  • 42
  • 54