65

I have one Android Device running Jelly Bean OS.

Is there any way to detect the process is running or not using ADB command if i know the package name?

RPB
  • 16,006
  • 16
  • 55
  • 79

6 Answers6

90

No need to use grep. ps in Android can filter by COMM value (last 15 characters of the package name in case of java app)

Let's say we want to check if com.android.phone is running:

adb shell ps m.android.phone
USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
radio     1389  277   515960 33964 ffffffff 4024c270 S com.android.phone

Filtering by COMM value option has been removed from ps in Android 7.0. To check for a running process by name in Android 7.0 you can use pidof command:

adb shell pidof com.android.phone

It returns the PID if such process was found or an empty string otherwise.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • 4
    Note that this will fail if the last 15 characters of a package name start with a number. (Example: com.number13.example.debug, where the last 15 chars are "3.example.debug"). Reason? The command filters by pid instead of process name if the first character is a digit: https://github.com/android/platform_system_core/blob/master/toolbox/ps.c#L295 – Madis Pink May 18 '15 at 05:11
  • A note for `pidof`: I'm sometimes getting "deleted" processes when I `readlink /proc/$PID/exe`. – jozxyqk Apr 12 '17 at 01:37
  • Great! Thanks! it's worked on Android 7.0 (Galaxy S8+) & 8.0 (emulator) – hungtdo Nov 09 '17 at 16:30
  • But need root device or current progress not at all! :( – hungtdo Nov 09 '17 at 18:12
  • For windows user adb shell "ps | grep apps | awk '{print $9}'" – S. Ber Rosenberg Nov 27 '22 at 19:47
34

You can use

adb shell ps | grep apps | awk '{print $9}'

to produce an output like:

com.google.process.gapps
com.google.android.apps.uploader
com.google.android.apps.plus
com.google.android.apps.maps
com.google.android.apps.maps:GoogleLocationService
com.google.android.apps.maps:FriendService
com.google.android.apps.maps:LocationFriendService

adb shell ps returns a list of all running processes on the android device, grep apps searches for any row with contains "apps", as you can see above they are all com.google.android.APPS. or GAPPS, awk extracts the 9th column which in this case is the package name.

To search for a particular package use

adb shell ps | grep PACKAGE.NAME.HERE | awk '{print $9}'

i.e adb shell ps | grep com.we7.player | awk '{print $9}'

If it is running the name will appear, if not there will be no result returned.

rcbevans
  • 7,101
  • 4
  • 30
  • 46
  • Thanks for the reply, i just want the process list like how it shows in DDMS tools under the device in Eclipse. – RPB May 22 '13 at 12:23
  • so i should execute adb shell dumpsys activity processes | grep apps | awk '{print $9}' ??? – RPB May 22 '13 at 13:11
  • No sorry, I have changed my answer, use what is shown above, adb shell ps | grep apps | awk '{print $9}' – rcbevans May 22 '13 at 16:33
  • The basic idea is sound, but this sequence of commands is only likely to work on custom roms or with a busybox install, as official Android releases only recently had `grep` added, and still do not have `awk` – Chris Stratton May 22 '13 at 16:42
  • You run it from your rig shell, its nothing to do with android having grep as it does the grep and awk on the local machine, not the device – rcbevans May 22 '13 at 18:23
  • 1
    Terminal Emulator in my Nexus 6 running 6.0 says awk not found. Am I missing something here? – Hendra Anggrian Mar 29 '16 at 19:15
  • @HendraAnggrian When you use adb shell (on your computer which you connect the phone to, with adb / USB debugging enabled in the phone's developer options), and type the commands above, `adb shell ps | ...` runs `ps` on your phone via adb, but the pipe `|` is set up by the shell on your computer, and `...` (including `awk`) run on your computer. If you want to do everything in the Terminal Emulator on the phone, this should work: `ps | while read a b c d e f g h i; do echo $i; done` (assuming ps output has 9 fields). Use `ps | tail -n +2 | ...` to avoid printing the header. – Gene Pavlovsky Nov 14 '16 at 22:40
  • 1
    @GenePavlovsky, no need to use `read`. simply remove sequential whitespaces with `tr -s ' '` like this `adb shel "ps | tr -s ' ' | cut -d' ' -f9 | grep '^com.google.android.gms$'"` – Alex P. Dec 09 '16 at 23:45
  • I'm getting this '/system/bin/sh: awk: not found' – user2934930 Oct 25 '17 at 13:56
  • A terminal window on Linux or OSX will have grep and awk. On Windows, you need a bash shell such as Git Bash, CygWin, or Windows Bash (WSH). – SteveS Feb 05 '20 at 22:17
30

If you want to directly get the package name of the current app in focus, use this adb command -

adb shell "dumpsys activity activities | grep mResumedActivity | cut -d "{" -f2 | cut -d ' ' -f3 | cut -d "/" -f1"

Extra info from the result of the adb command is removed using the cut command. Original solution from here.

vepzfe
  • 4,217
  • 5
  • 26
  • 46
  • 1
    I love this one, although I had to use -f 6 Does it always produce the same output? – Arnaldo Capo Mar 22 '17 at 18:34
  • @ArnaldoCapo For me it doesn't work with -f 6. Without cut command, this is the full output I get : mFocusedApp=AppWindowToken{eb42ebb token=Token{f96bf4a ActivityRecord{d040cb5 u0 com.package/.MainNavBarActivity t8679}}} So, -f 6 produces "u0". I'm haven't checked, but I hope it produces the same output consistently across devices and android versions. Could you paste your entire output without cut? – vepzfe Mar 23 '17 at 08:36
  • Worked like magic. Thanks! – Nisim Naim Dec 12 '18 at 12:23
  • `mFocusedApp` didn't work for me on Android 11 - the output of the `dumpsys` didn't contain any occurrence of `mFocusedApp` at all. Instead I had success using `mObscuringWindow`, which seems to work in the same way! – Luke Needham Feb 01 '23 at 10:25
  • @LukeNeedham thanks for pointing this out. I couldn't get the `mObscuringWindow` solution to work but found another way which works on Android 7, 11 & 12 as far as I've tested. If the `mObscuringWindow` method worked, can you please share that as well. – vepzfe Feb 13 '23 at 08:47
  • I got the command - `adb shell dumpsys window windows | grep -E 'mObscuringWindow'| cut -d / -f 1 | cut -d " " -f5`. However, this command doesn't seem to be working on my Android 11 Pixel. Neither does it work on Android 7 Motorola. Only seems to work on Android 12 OnePlus. However, the new solution I posted works on all devices. – vepzfe Feb 13 '23 at 08:54
16

I just noticed that top is available in adb, so you can do things like

  adb shell
  top -m 5

to monitor the top five CPU hogging processes.

Or

  adb shell top -m 5 -s cpu -n 20 |tee top.log

to record this for one minute and collect the output to a file on your computer.

StefanQ
  • 708
  • 10
  • 16
8

Try:

adb shell pidof <myPackageName>

Standard output will be empty if the Application is not running. Otherwise, it will output the PID.

Chris Sears
  • 311
  • 3
  • 2
3

Alternatively, you could go with pgrep or Process Grep. (Busybox is needed)

You could do a adb shell pgrep com.example.app and it would display just the process Id.

As a suggestion, since Android is Linux, you can use most basic Linux commands with adb shell to navigate/control around. :D

deubaka
  • 1,447
  • 12
  • 22