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]
Related
I am trying to debug an Android application using command line and script tools (I've had enough of Android Studio eating my CPU).
To connect to my app, I do adb jdwp to get the pid and then adb forward tcp:8700 jdwp:<pid> to be able to connect to the app. But I'd like to script this more.
adb jdwp have two limitations: 1. It doesn't return, so I have to kill adb with ctrl-c, 2. it only lists pids. If there are more pids, there's no easy way to see which is my app.
Is there a way to get adb jdwp to behave: Get it to exit and to return applicationId along with the pid?
Here is an example script just like Struchu's without writing to a local file
Improvements
It starts the app
It gets the apps program id after starting it
It also uses adb over wifi given that your device is starting the hotspot
#!/bin/bash
PACKAGE="com.example.app"
ACTIVITY="$PACKAGE"/"$PACKAGE"".MainActivity"
#Connect over wifi
adb connect "$(ip r | grep default | cut -f3 -d\ )":5555
# adb -e connect to wifi device and start activity
adb -e shell am start -D -n "$ACTIVITY"
JDWP=$(adb -e shell ps | grep "$PACKAGE" | cut -f7 -d\ )
adb forward tcp:8000 jdwp:"$JDWP"
jdb -sourcepath app/src/main/java -attach localhost:8000
I managed to solve this issue with the following script (which will work provided your app is launched as a last process). As a future enhancement it could be integrated with gradle task runner to simplify the process even more.
#!/bin/bash
DEBUG_PORT=7777
SOURCE_PATH=app/src/main/java
FILE=/var/tmp/andebug-$(date +%s)
adb jdwp > "$FILE" &
sleep 1
kill -9 $!
JDWP_ID=$(tail -1 "$FILE")
rm "$FILE"
adb forward tcp:$DEBUG_PORT jdwp:$JDWP_ID
jdb -sourcepath $SOURCE_PATH -attach localhost:$DEBUG_PORT
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 use the following command to start playing a movie on my android device:
adb shell am start -d file:///sdcard/fff.wvm -t video/3gp -a android.intent.action.VIEW
I want to stop (kill) the process before it finishes. Does anyone know the process name "am start" command create? I used ps to get the running processes in the background, but there is a lot, and it is hard to tell which one is.
If you know the package being started, use:
adb shell am force-stop <PACKAGE>
For me com.android.gallery worked pretty fine, however this depends on the installed app and preferred apps, of course. Going i.e. pushing Back might end the playback - it least it worked with the system player:
adb shell input keyevent 4
Appending the command ...
adb shell am kill-all
... should kill the process afterwards.
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 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>