Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
956 views
in Technique[技术] by (71.8m points)

android - 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/ss*/ /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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...