通过程序包名称查找正在运行的进程ID [英] Find out the running process ID by package name

查看:179
本文介绍了通过程序包名称查找正在运行的进程ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个脚本,在其中需要提供应用程序的PID。我可以通过以下命令列出所有具有其PID的进程,并且可以看到我的应用程序的条目。


adb shell ps


这给了我一大堆过程。而且我需要一个条目(我可以将其进一步提供给另一个命令),因此我想使用包名称来过滤此结果。 grep命令在Windows计算机上不起作用。还尝试了以下命令,但没有帮助。


adb shell ps名称:my_app_package



解决方案

自Android 7.0起,按包名称查找进程ID的最简单方法是使用 pidof 命令:

 用法:pidof [-s] [-o omitpid [,omitpid ...]] [ NAME] ... 

使用给定名称打印所有进程的PID。

-s单发,仅返回一个pid。
-o省略PID($)

只需要这样运行:

  my.app.package $ a $ shell pid 

在7.0之前的Android中,人们使用 ps 命令,然后使用内置过滤器通过 comm 值(对于android应用而言,是包名称的最后15个字符)或 grep 命令。如果名称的后15个字符以数字开头且未包含 grep ,则 comm 过滤器将不起作用默认情况下直到Android 4.2。但是,即使在找到正确的生产线之后,仍需要提取 PID 值。



有多种方法要做到这一点。以下是使用单个 sed 命令可以找到过程并提取PID的方法:

  adb shell ps | sed -n's / ^ [^] * * \([0-9] * \)。* my\.app\.package $ / \1 / p' 

同样的问题是 sed



但是如果您必须使用旧设备,则可以始终使用以下独立于Android版本的解决方案。它不使用任何外部命令-只是内置了Android shell:

  adb shell,用于/ proc / [0 -9] *;执行[[$(< $ p / cmdline)= my.app.package]]&& echo $ {p ## * /};完成 

寻找PID的最常见原因是在其他命令中使用它,例如 kill 。假设我们有多个 logcat 实例正在运行,我们希望一次正常完成所有实例。只需在最后一个命令中将 echo 替换为 kill -2

  adb shell,用于/ proc / [0-9] *中的p;执行[[$(< $ p / cmdline)= logcat]]&& kill -2 $ {p ## * /};完成 

替换 '(如果从Linux / OSX Shell运行命令)。


I am working on a script in which I need to supply the PID of my application. I am able to list all the processes with their PIDs by following command and could see the entry of my application.

adb shell ps

This gives me a huge list of processes. And I need a single entry (which I can further supply to another command), so I want to filter this results with a package name. The grep command does not work on my windows machine. Also tried following command but it didn't help.

adb shell ps name:my_app_package

解决方案

Since Android 7.0 the easiest way to find out the process ID by package name is to use pidof command:

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)

Just run it like this:

adb shell pidof my.app.package

In Android before 7.0 people used ps command and then parsed its output using either built-in filter by comm value (which for android apps is the last 15 characters of the package name) or grep command. The comm filter did not work if the last 15 characters of the name started with a digit and the grep was not included by default until Android 4.2. But even after the proper process line was found the PID value still needed to be extracted.

There were multiple ways to do that. Here is how finding the process and extracting PID could be done with a single sed command:

adb shell "ps | sed -n 's/^[^ ]* *\([0-9]*\).* my\.app\.package$/\1/p'"

Again the problem is that sed was was not included by default until Android 6.0.

But if you must use an older device you can always use the following Android version independent solution. It does not use any external commands - just Android shell built-ins:

adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = my.app.package ]] && echo ${p##*/}; done"

The most popular reason for looking for a PID is to use it in some other command like kill. Let's say we have multiple instances of logcat running and we want to finish them all gracefully at once. Just replace the echo with kill -2 in the last command:

adb shell "for p in /proc/[0-9]*; do [[ $(<$p/cmdline) = logcat ]] && kill -2 ${p##*/}; done"

Replace " with ' if running the commands from Linux/OSX shell.

这篇关于通过程序包名称查找正在运行的进程ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆