I am able to start native applications using am start -a action -n packagename/activity. How can I kill/stop a native application from adb shell?
adb shell am force-stop packagename
Chirag deleted it, so here it is again:
adb shell ps | grep com.myapp | awk '{print $2}' | xargs adb shell kill
This is to be run outside of the emulator. It is one long Unix command, not four commands with a visual separation. | is syntax, interpreted by your (Ubuntu's) shell, which then pipes the output from adb, grep, etc., into the next. Only ps is executed in the emulator.
Another way to kill your app is to send your app to backround (using home button) and call:
adb shell am kill com.your.package
It works not on all of my devices, however, on devices where it works it behaves the same way as if the app was killed in background due to lack of resources and this state is often handy to test different aspects of your process recreation.
For example, if you have Broadcast Receivers registered in Manifest, they will still start without restarting your app manually, comparing to force stop that you can achieve using:
adb shell am force-stop com.your.package
Please try the below command in adb shell.
adb shell kill <PID>
Related
I try to find some way to kill unnecessary services/processes in Android from shell.
The problem is that after killing the process it starts again after few seconds!
for example I tried to kill batterywarning, but it keep starting again:
root#w812a_kk:/ # ps | grep batteryw
shell 17986 1 1044 364 c00601dc b6e9f094 S /system/bin/batterywarning
1|root#w812a_kk:/ # ps | grep batteryw
shell 17781 1 1044 364 c00601dc b6ee6094 S /system/bin/batterywarning
root#w812a_kk:/ # busybox killall batterywarning
root#w812a_kk:/ # ps | grep batteryw
1|root#w812a_kk:/ # ps | grep batteryw
shell 17986 1 1044 364 c00601dc b6e9f094 S /system/bin/batterywarning
I did find several methods to kill service/process in the following link, yet the process is starting again.
Android ADB stop application command like "force-stop" for non rooted device.
Is it something that can only be done in init.rc ?
Thanks,
This service is probably started as STICKY - it will be automatically restarted by OS after some predefined timeout.
You can check it by pulling the logcat from the device and grepping it for the name of the service you attempted to kill:
$ adb logcat -d > logcat.txt
$ grep -C 5 -i batterywarning logcat.txt
If you see something along the lines scheduling restart of crashed service in X seconds then you know for sure that the service is being restarted by OS.
If it is indeed the case, I doubt that you can kill it completely on a non-rooted device (neither I know how to achieve this on a rooted one).
kill a process when you have the process name (basically you don't have to search 'ps' for the process ID and then kill it with PID):
adb shell kill $(pidof com.android.phone)
Use the following command from terminal:
adb shell am crash [applicationId]
I really need help here...
I am writing a shell script, which starts a process then kills it every 10 seconds then restarts it again.
I understand that using 'ps' command will show all the processes that are running and you can kill it by running 'kill [pid]'.
However, grepping the PID of the process that I am launching is not easy.
I've looked everywhere and for some reason, I cannot use functions like pgrep, awk, xargs, pidof... They are just not found...
I can only think of a way where I have to output the ps file then parse it and grab the PID alone.. but that seems too much...
Can anyone help me? I think I am only limited to using ps and grep only...
I am launching the phone application by running
am start -a android.intent.action.CALL -d tel:XXX-XXX-XXXX
Then by running 'ps m.android.phone' I can use kill [PID] to stop call the call
I've also tried running commands like
pm clear com.android.phone,
adb shell am force-stop com.android.phone
but none of them would stop the call...
Please help
Thanks a lot!
Use
am kill package-name
or
am force-stop package-name
replacing package-name by the name of the application you want to kill.
If you know the process name of the application, then I wrapped together a very quick and dirty script to parse the PID of the process:
ps -u $(whoami) | grep firefox | awk '{printf $1}'
You should obviously replace firefox with your process name of choice.
Please note that I am no expert on the area, but it works on my end.
You could try the command pidof to get the process id directly, eks:
pidof apashe2
following what marcus said:
adb shell kill -9 $(adb shell ps | grep firefox | awk '{print $1}')
I'm trying to stop application on Android 2.3.7 device. But in this version of Android I can't use "force-stop" command. Do you know any other ways to close application on non rooted device?
The first way
Requires root
Use kill:
adb shell ps => Will list all running processes on the device and their process ids
adb shell kill <PID> => Instead of <PID> use process id of your application
The second way
In Eclipse open DDMS perspective.
In Devices view you will find all running processes.
Choose the process and click on Stop.
The third way
It will kill only background process of an application.
adb shell am kill [options] <PACKAGE> => Kill all processes associated with (the app's package name). This command kills only processes that are safe to kill and that will not impact the user experience.
Options are:
--user <USER_ID> | all | current: Specify user whose processes to kill; all users if not specified.
The fourth way
Requires root
adb shell pm disable <PACKAGE> => Disable the given package or component (written as "package/class").
The fifth way
Note that run-as is only supported for apps that are signed with debug keys.
run-as <package-name> kill <pid>
The sixth way
Introduced in Honeycomb
adb shell am force-stop <PACKAGE> => Force stop everything associated with (the app's package name).
P.S.: I know that the sixth method didn't work for you, but I think that it's important to add this method to the list, so everyone will know it.
If you have a rooted device you can use kill command
Connect to your device with adb:
adb shell
Once the session is established, you have to escalade privileges:
su
Then
ps
will list running processes. Note down the PID of the process you want to terminate. Then get rid of it
kill PID
If you want to kill the Sticky Service,the following command NOT WORKING:
adb shell am force-stop <PACKAGE>
adb shell kill <PID>
The following command is WORKING:
adb shell pm disable <PACKAGE>
If you want to restart the app,you must run command below first:
adb shell pm enable <PACKAGE>
To kill from the application, you can do:
android.os.Process.killProcess(android.os.Process.myPid());
I hope to stop all backgroud processes.
I got to know that adb shell am kill-all may do this job. I exit one application by pressing BACK,
then I enter adb shell am kill-all, However, I find the process by entering adb shell ps | grep packagename.
Does adb shell am kill-all really work?
If you don't have to kill background process with adb shell command. You can use some task manager in play store.
I also developed a task manager by myself, there are source codes:
https://code.google.com/p/my-test-project-lion/
I'm using the monkey tool to run a test of my Android application. For example, I might do a run like the following:
adb shell monkey -p com.myapp -v 10000
However, if I change my mind and need to cancel the test, there doesn't seem to be a way to do so that doesn't involve waiting multiple minutes for the damned monkey to finish most or all of its run.
Killing the adb shell process on my mac doesn't solve the problem. Killing the com.myapp process on my phone using ddms doesn't work. Unplugging my phone doesn't work.
How do I cancel the monkey madness?
You can kill the monkey process just doing this:
$ adb shell ps | awk '/com\.android\.commands\.monkey/ { system("adb shell kill " $2) }'
[Nitpick] You're confusing monkeyrunner with monkey.
The monkeyrunner tool is not related to the UI/Application Exerciser
Monkey, also known as the monkey tool. The monkey tool runs in an adb
shell directly on the device or emulator and generates pseudo-random
streams of user and system events. In comparison, the monkeyrunner
tool controls devices and emulators from a workstation by sending
specific commands and events from an API.
[/Nitpick]
On my Android 2.2 device when I start monkey, I see a process started in DDMS by the name "?" (just a question mark). When I killed that process, the monkey madness stopped.
adb shell
ps | grep monkey
kill process_id
adb shell kill $(adb shell pgrep monkey)
kudo to #deadfish
For what it's worth, I use Android Studio 3.1.4 on a Mac in 2018 and I had to alter the accepted answer like so:
./adb shell ps | awk '/com\.android\.commands\.monkey/ { system("./adb shell kill " $2) }'
Hope that help prevent some hair-pulling and pencil snapping out there!
Also... when it comes to the monkey, always be sure to pin your app!!! Otherwise you might accidentally send all your selfies to a random email in China like I did. ¯\_(ツ)_/¯
Kill the monkey by shell will cause a small problem, the IActivityController in ActivityTaskManagerService will not be set to null, which it should. And the ActivityManager.isUserAMonkey() still return true.
If monkey stop automatically, it will reset the Controller properly:
Monkey.java{
private int run(String[] args) {
...
try {
mAm.setActivityController(null, true);
mNetworkMonitor.unregister(mAm);
}
...
}
}