This question already has an answer here:
-
Find out the running process ID by package name
3 answers
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?
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.
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.
If you want to directly get the package name of the current app in focus, use this adb command -
adb shell dumpsys window windows | grep -E 'mFocusedApp'| cut -d / -f 1 | cut -d " " -f 7
Extra info from the result of the adb command is removed using the cut command. Original solution from here.
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.
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
Try:
adb shell pidof <myPackageName>
Standard output will be empty if the Application is not running. Otherwise, it will output the PID.