Is it possible to kill ALL the active tasks/apps in the task manager using ADB? This would be equivalent of opening the task manager and killing each task one by one...
I tried using the the following adb shell command but that didn't kill all the task.
adb shell am kill-all
I can't use the adb shell am force-stop <PACKAGE> command because it would require me to know which package/app is running. I want to kill ALL the user apps task that are running. Similarly to using the task manager and killing each task one by one.
According to the command description, kill-all kills all background processes. Are background processes equivalent to "services" and task equivalent to "activities"?
Also, is it possible to clear cache of apps using ADB while keeping the user data? I seems that the adb shell pm clear clears all the user data. I want to only clear the cache.
The reason why I am asking is because I am doing some performance testing on few user apps. To make each test valid, I want to ensure none of the user apps have any task, activities, services, and cache already in the background.
You can use force-stop, it doesn't require root permission.
adb shell am force-stop <PACKAGE>
And you can get the package name from the top running activity/app
adb shell "dumpsys activity | grep top-activity"
After that you need to play a bit with the result, to extract the package, here my java code that does that:
public void parseResult(String line){
int i = line.indexOf(" 0 ");
if(i == -1){
return;
}
line = line.substring(i);
i = line.indexOf(":");
if(i == -1){
return;
}
line = line.substring(i + 1);
i = line.indexOf("/");
return line.substring(0, i);
}
If you want to start clean slate i.e close the app and clear its data too you can do the following
adb shell pm clear com.yourapp.package
For non rooted devices I expanded on Faisal Ameer's Script
adb shell ps | grep -v root | grep -v system | grep -v "android.process." | grep -v radio | grep -v "com.google.process." | grep -v "com.lge." | grep -v shell | grep -v NAME | awk '{print $NF}' | tr '\r' ' ' | xargs adb shell am force-stop
The adb shell am force-stop does not require root permission. Note that the applications still show up in the devices running application drawer but I have verified that the packages processes have been cleared using.
adb shell dumpsys meminfo relevant.package.names
find running apps, ignoring system apps etc and kill, the following command does it all;
adb shell ps|grep -v root|grep -v system|grep -v NAME|grep -v shell|grep -v smartcard|grep -v androidshmservice|grep -v bluetooth|grep -v radio|grep -v nfc|grep -v "com.android."|grep -v "android.process."|grep -v "com.google.android."|grep -v "com.sec.android."|grep -v "com.google.process."|grep -v "com.samsung.android."|grep -v "com.smlds" |awk '{print $2}'| xargs adb shell kill
you can add more exceptions if you find any like this; grep -v "exception"
Related
I was trying to kill my camera process in adb (i'm running android R) using this command:
kill -HUP ps -A | grep camera | awk '{print $2}'
but after i hit enter, it gave me an error:
/system/bin/sh: kill: ps: arguments must be jobs or process IDs
/system/bin/sh: kill: -A: arguments must be jobs or process IDs
Any idea on how to fix this?
Suggest to use command pkill and learn about pgrep as well.
pkill -9 -f "camera"
I would like to find a specific target's pid and kill this process.
Then, i made a shell:
#!/bin/bash
#
func(){
while true; do
adb shell ps | grep my-target-process | awk '{print $2}' | xargs adb shell kill
sleep 1
done
}
But, i always get errors like below :
usage: kill [-s signame | -signum | -signame] { job | pid | pgrp } ...
kill -l [exit_status ...]
Is there anything wrong? Thanks.
I will add an explanation the logic I suggested in the comments section. The reason why
| xargs -I{} adb shell kill "{}"
works is, with the -I{} flag in xargs, the {} becomes a placeholder for the output returned from the previous command, i.e. the process-id is now present in {} and can be passed as an argument/input to your kill command.
(also) As an alternative, if your Android version supports pidof command, which returns the process-id pid directly, you can do
adb shell pidof -s "my-target-process" | xargs -I{} kill "{}"
Using the following 3 commands, Can we have single adb shell command to know the current apk file location and version of the apk.
we can use following command to find the all packages and locations of the apks
adb shell pm list packages -f
we can use following command to know the current package name.
adb shell dumpsys window | grep -i mCurrentFocus
we can use following command to know apk version
adb shell dumpsys package my.package | grep versionName
This will be use full for every one to quickly check the current apk details immediately.
Example: If i open com.src.test application from device. i want to know where this apk installed (/system/app/) and version of apk (1.0101) using single adb shell command.
Something like this maybe?
package=$(dumpsys window windows | grep mCurrentFocus | cut -d'/' -f1 | rev | cut -d' ' -f1 | rev) && dumpsys package $package | grep -E "versionName|codePath"
codePath=/data/app/com.src.test
versionName=1.0101
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/\s\s*/ /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
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)
Not sure if you can get the PID directly however you can try the following
set `ps |grep android.process.acore`
echo $2
This has the affect of setting the output of the ps command into variables $1, $2, $3 etc. The PID value is in $2
I tried this one and it seems to work:
adb shell "set "ps | grep android.process.media"; kill -9 $2"
adb shell pidof [package name]
or
adb shell pidof -s [package name]
-s option is for single shot, returning only one pid.
I tried this one and it seems to work:
adb shell
ps -A | grep "android.process.acore"
Is there a way of running adb commands on all connected devices? To uninstall an app from all connected devices with "adb uninstall com.example.android".
The commands I am interested in is mainly install and uninstall.
I was thinking about writing a bash script for this, but I feel like someone should have done it already :)
Create a bash file and name it e.g. adb+:
#!/bin/bash
adb devices | while read -r line
do
if [ ! "$line" = "" ] && [ "$(echo "$line" | awk '{print $2}')" = "device" ]
then
device=$(echo "$line" | awk '{print $1}')
echo "$device" "$#" ...
adb -s "$device" "$#"
fi
done
Usage: ./adb+ <command>
Building on #Oli's answer, this will also let the command(s) run in parallel, using xargs. Just add this to your .bashrc file:
function adball()
{
adb devices | egrep '\t(device|emulator)' | cut -f 1 | xargs -t -J% -n1 -P5 \
adb -s % "$#"
}
and apply it by opening a new shell terminal, . ~/.bashrc, or source ~/.bashrc.
If you only want to run on devices (or only on emulators), you can change the (device|emulator) grep by removing the one you don't want. This command as written above will run on all attached devices and emulators.
the -J% argument specifies that you want xargs to replace the first occurrence of % in the utility with the value from the left side of the pipe (stdin).
NOTE: this is for BSD (Darwin / Mac OS X) xargs. For GNU/Linux xargs, the option is -I%.
-t will cause xargs to print the command it is about to run immediately before running it.
-n1 means xargs should only use at most 1 argument in each invocation of the command (as opposed to some utilities which can take multiple arguments, like rm for example).
-P5 allows up to 5 parallel processes to run simultaneously. If you want instead to run the commands sequentially, simply remove the entire -P5 argument. This also allows you to have two variations of the command (adball and adbseq, for example) -- one that runs in parallel, the other sequentially.
To prove that it is parallel, you can run a shell command that includes a sleep in it, for example:
$ adball shell "getprop ro.serialno ; date ; sleep 1 ; date ; getprop ro.serialno"
You can use this to run any adb command you want (yes, even adball logcat will work! but it might look a little strange because both logs will be streaming to your console in parallel, so you won't be able to distinguish which device a given log line is coming from).
The benefit of this approach over #dtmilano's & approach is that xargs will continue to block the shell as long as at least one of the parallel processes is still running: that means you can break out of both commands by simply using ^C, just like you're used to doing. With dtmilano's approach, if you were to run adb+ logcat, then both logcat processes would be backgrounded, and so you would have to manually kill the logcat process yourself using ps and kill or pkill. Using xargs makes it look and feel just like a regular blocking command line, and if you only have one device, then it will work exactly like adb.
This is an improved version of the script from 強大な. The original version was not matching some devices.
DEVICES=`adb devices | grep -v devices | grep device | cut -f 1`
for device in $DEVICES; do
echo "$device $# ..."
adb -s $device $#
done
To add in the ~/.bashrc or ~/.zshrc:
alias adb-all="adb devices | awk 'NR>1{print \$1}' | parallel -rkj0 --tagstring 'on {}: ' adb -s {}"
Examples:
$ adb-all shell date
$ adb-all shell getprop net.hostname
$ adb-all sideload /path/to/rom.zip
$ adb-all install /path/filename.apk
$ adb-all push /usr/local/bin/frida-server-arm64 /data/local/tmp/frida-server
Explanation: awk extracts the device id/host (first column: print $1) of every lines except the first one (NR>1) to remove the "List of devices attached" header line), then gnu parallel runs adb -s <HOSTNAME> <whatever-is-passed-to-the-alias> on whatever non-empty line (-r) in the order specified (-k, to avoid random order / fastest response order) and prepend each line with on <DEVICE>:\t for clarity, all in parallel (-j0, possible to set another number to define how many adb should be ran in parallel instead of unlimited).
:)
This is the highest result on Google, so for all Windows users coming here let me add this solution by User zingh (slightly modified to accept arbitrary commands, rather than "only" install
Batch file (adball.bat):
FOR /F "skip=1" %%x IN ('adb devices') DO start adb -s %%x %*
Call as:
adball uninstall com.mypackage
(%* takes all input parameters, my line above makes it so that all commands are passed to adb as they are, so that you can type multiple words, flags etc.)
Note: you can even use this directly from the Android Studio "run all" popup, if you install the Powershell-plugin. You can add adball to your path, then double-tap ctrl and run
powershell adball uninstall com.mypackage
adb wrapper supports selecting multiple targets for adb commands and parallel execution.
From its README:
# Installation
./install.sh ~/apps/android-sdk-linux
# Execute adb commands on all connected devices.
adb set-target all
# Execute adb commands on given devices.
adb set-target emulator-5554 C59KGT14263422
# Use GNU parallel for parallel install.
adb set-parallel true
(Disclaimer: I have written half of it)