Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adb: Find PID from the adb shell

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

like image 767
rumit patel Avatar asked Jan 23 '14 21:01

rumit patel


People also ask

How do I find the process ID in adb shell?

adb shell. ps -A | grep "android. process. acore"

How do I get the PID of my running app?

In the Start menu search bar, search for command prompt and select Run as administrator. Type tasklist. Press Enter. Command Prompt will now display the PID for the running processes.

How do I list files in adb shell?

Open cmd type adb shell then press enter. Type ls to view files list. At the DOS prompt, adb shell ls -R > junk lists all files and puts results into file junk which you can then edit via Notepad or whatever and see more than you'd want to, but your files, too!

What is adb shell command?

Android Debug Bridge (adb) is a versatile command-line tool that lets you communicate with a device. The adb command facilitates a variety of device actions, such as installing and debugging apps, and it provides access to a Unix shell that you can use to run a variety of commands on a device.


2 Answers

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)
like image 129
Alex P. Avatar answered Oct 30 '22 12:10

Alex P.


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

like image 43
Steve Weet Avatar answered Oct 30 '22 12:10

Steve Weet