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}')
Related
I'm currently doing this:
-d: call logcat only once and return
adb shell logcat -d
Then I do a regex search in the log for Caused by.*"apk name".
The problem is, sometimes it does generate this message in the log but the app is still responsive/working.
Would like to ask if there's any better way to check if an app stop working, using adb shell commands.
By stop working I mean, it still open but doesn't responds to anything.
Note: I'm not looking for what caused the app to crash, just if it has stopped working.
Did you know that:
Android stores trace information when it experiences an App is Not Responding (ANR).
You can get more information here: https://developer.android.com/topic/performance/vitals/anr#diagnosing_anrs
With this information, I was able to determine if the app is in ANR state via the following commands:
First, get the PID of your app:
adb shell ps | grep <app.package.name>
The value in second column is the PID. Then, enable root access:
adb root
Finally, check the /data/anr/:
adb shell grep <PID> /data/anr/*<app.package.name>*
Note that the asterisks (*) in the last command are important. If you see something like below, your app is in ANR state.
/data/anr/traces_app.package.name.txt:----- pid 11828 at 2021-09-06 03:00:05 -----
/data/anr/traces_app.package.name.txt: | sysTid=11828 nice=0 cgrp=default sched=0/0 handle=0xb6f5cbec
/data/anr/traces_app.package.name.txt:----- end 11828 -----
you can try :
adb shell pid of the app
It returns the PID if such process was found or an empty string otherwise.
for pid u can use adn shell ps
like user1506104 suggests: looking for the deovr player on my oculus quest 2:
... MINGW64 /e/pt
$ ./adb shell ps | grep com.deovr.gearvr
u0_a90 3936 695 3212880 614872 0 0 S com.deovr.gearvr
$ ./adb shell ps -p 3936
USER PID PPID VSZ RSS WCHAN ADDR S NAME
u0_a90 3936 695 3212880 616096 0 0 S com.deovr.gearvr
You can use also command for directly knowing what is happening with error stack and you will be also able to reach any of crashes:
adb logcat *:E
Here you can get also more options:
https://developer.android.com/studio/command-line/logcat#options
Getting actual ANR stack - check this thread how to obtain it from shell:
Android: How to obtain data/anr/traces.txt from Samsung Galaxy Tab?
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 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);
}
...
}
}
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>