Linux adb:从 adb shell 中查找 PID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21319883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adb: Find PID from the adb shell
提问by rumit patel
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
我正在尝试获取 INSIDE adb shell 进程的 PID。所以,我正在做 adb shell,它让我进入 android shell。现在,如果我要使用常规外壳获取 PID,我会使用
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 }'
I get the PID (a numeric number - 2nd field of the ps | grep android.process.acore
) output.
我得到了 PID(一个数字 - 的第二个字段ps | grep android.process.acore
)输出。
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.
但是,如果我在 android shell 中运行上述命令(在执行之后adb shell
),我会分别得到/system/bin/sh: sed: not found
和/system/bin/sh: awk: not found
错误。这意味着,这些命令在 adb shell 中不可用。但是,grep 有效。
The output of the ps | grep android.process.acore
inside adb shell
is:
ps | grep android.process.acore
里面的输出adb shell
是:
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
?
我正在寻找数字 11826。我怎样才能在里面提取它adb shell
?
Also, please help if there is a direct way to get the PID inside the adb shell.
另外,如果有直接的方法可以在 adb shell 中获取 PID,请提供帮助。
Regards, Rumit
问候, 鲁米特
采纳答案by Steve Weet
Not sure if you can get the PID directly however you can try the following
不确定您是否可以直接获取 PID,但是您可以尝试以下操作
set `ps |grep android.process.acore` echo
This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2
这会影响将 ps 命令的输出设置为变量 $1、$2、$3 等。PID 值在 $2 中
回答by y0tta
I tried this one and it seems to work:
我试过这个,它似乎有效:
adb shell "set "ps | grep android.process.media"; kill -9 "
回答by Alex P.
Android versions starting with 6.0 already include pidof
utility:
从 6.0 开始的 Android 版本已经包含pidof
实用程序:
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)