Android Stop Emulator from Command Line

AndroidAndroid Emulator

Android Problem Overview


This question is identical to https://stackoverflow.com/questions/5912403/how-to-shut-down-android-emulator-via-cmd.

However, after attempting the suggested solution from the first answer adb emu kill has not proven successful for me.

I am automating unit tests for an android application. My bash script runs on a headless machine. It creates an android device using android create avd and executes emulator with the -no-window attribute. It then compiles the test project, connects to the emulator using adb, installs the project and executes my tests. This all works fine.

Now I need to terminate the emulator process, and just like the referenced post, I am only able to do this using kill -9.

The Google tutorial Managing AVDs from the Command Line only mentions how to stop emulators within a GUI environment.

Any help is appreciated.

Android Solutions


Solution 1 - Android

May be adb kill-server helps for you?

or

adb -s emulator-5544 emu kill, where emulator-5544 - emulator name.

For Linux users it will be

adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done

Solution 2 - Android

To stop all running emulators we use this command:

adb devices | grep emulator | cut -f1 | while read line; do adb -s $line emu kill; done

Solution 3 - Android

FOR MAC:

  1. Run:
ps -ax | grep emulator 

which gives you a wide result. Something like:

 6617 ??         9:05.54 /Users/nav/Library/Android/sdk/emulator/qemu/darwin-x86_64/qemu-system-x86_64 -netdelay none -netspeed full -avd Nexus_One_API_29
 6619 ??         0:06.10 /Users/nav/Library/Android/sdk/emulator/emulator64-crash-service -pipe com.google.AndroidEmulator.CrashService.6617 -ppid 6617 -data-dir /tmp/android-nav/
 6658 ??         0:07.93 /Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/QtWebEngineProcess --type=renderer --disable-accelerated-video-decode --disable-gpu-memory-buffer-video-frames --disable-pepper-3d-image-chromium --enable-threaded-compositing --file-url-path-alias=/gen=/Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/gen --enable-features=AllowContentInitiatedDataUrlNavigations --disable-features=MacV2Sandbox,MojoVideoCapture,SurfaceSynchronization,UseVideoCaptureApiForDevToolsSnapshots --disable-gpu-compositing --service-pipe-token=15570406721898250245 --lang=en-US --webengine-schemes=qrc:sLV --num-raster-threads=4 --enable-main-frame-before-activation --service-request-channel-token=15570406721898250245 --renderer-client-id=2
 6659 ??         0:01.11 /Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/QtWebEngineProcess --type=renderer --disable-accelerated-video-decode --disable-gpu-memory-buffer-video-frames --disable-pepper-3d-image-chromium --enable-threaded-compositing --file-url-path-alias=/gen=/Users/nav/Library/Android/sdk/emulator/lib64/qt/libexec/gen --enable-features=AllowContentInitiatedDataUrlNavigations --disable-features=MacV2Sandbox,MojoVideoCapture,SurfaceSynchronization,UseVideoCaptureApiForDevToolsSnapshots --disable-gpu-compositing --service-pipe-token=--lang=en-US --webengine-schemes=qrc:sLV --num-raster-threads=4 --enable-main-frame-before-activation --service-request-channel-token=  --renderer-client-id=3
10030 ttys000    0:00.00 grep emulator
  1. The first (left) column is the process ID (PID) that you are looking for.

  2. Find the PID in the first (top) row. In the above example, it's 6617.

  3. Kill that process:

kill PID

In my case, the command is:

kill 6617
  1. Usually, killing the first process in enough to stop the emulator, but if that doesn't work, you can:

    5.1. try killing other processes as well.

    5.2 kill with -9 (force kill):

kill -9 PID

Solution 4 - Android

if

 adb kill-server 

doesn't work. Use :

 adb emu kill

this will kill all the emulators

If multiple emulators are present then use:

adb -s * emu kill

Solution 5 - Android

Sometimes the command

adb -s emulator-5554 emu kill

did not work on my CI servers or desktops, for unknown reason. I think on Windows it's OK to kill the process of qemu, just like

Taskkill /IM qemu-system-x86_64.exe /F /T

Solution 6 - Android

I can close it with:

adb shell reboot -p

Solution 7 - Android

The other answer didn't work for me (on Windows 7). But this worked:

telnet localhost 5554
kill

Solution 8 - Android

Why not just do

adb reboot bootloader

Solution 9 - Android

adb kill-server will kill all emulators and restart the server clean.

Solution 10 - Android

None of the solutions worked for me. I had to go the telnet way including authentication:

AUTH=$(cat "$HOME/.emulator_console_auth_token")

expect << EOF
spawn telnet localhost 5554
expect "OK"
send   "auth $AUTH\r"
expect "OK"
send   "kill\r"
expect "OK"
send   "exit\r"
EOF

The full script can be obtained with a free license from https://github.com/kullo/android-emulator-tools


Update: looks like this still does not reliably close the console and ADB ports (e.g. 5554,5555)

Solution 11 - Android

I use this one-liner, broken into several lines for readability:

adb devices |
 perl -nle 'print $1 if /emulator-(\d+).device$/' |
 xargs -t -l1 -i bash -c "
   ( echo auth $(cat $HOME/.emulator_console_auth_token) ;
     echo kill ;
     yes ) |
   telnet localhost {}"

Solution 12 - Android

If you don't want to have to know the serial name of your device for adb -s emulator-5554 emu kill, then you can just use adb -e emu kill to kill a single emulator. This won't kill anything if you have more than one emulator running at once, but it's useful for automation where you start and stop a single emulator for a test.

Solution 13 - Android

List of devices attached emulator-5584 host emulator-5580 host emulator-5576 host emulator-5572 host emulator-5568 host emulator-5564 host emulator-5560 host

C:\Users\Administrator>adb -s emulator-5584 emu kill error: could not connect to TCP port 5584: cannot connect to 127.0.0.1:5584: No connection could be made because the target machine actively refused it. (10061)

NOTE: gui of emulator is not running but still it's showing

SOLUTION:

adb kill-server

start emulator using:

emulator.exe -netdelay none -netspeed full -avd Nexus_5X_API_19

Solution 14 - Android

To automate this, you can use any script or app that can send a string to a socket. I personally like nc (netcat) under cygwin. As I said before, I use it like this:

$ echo kill | nc -w 2 localhost 5554

(that means to send "kill" string to the port 5554 on localhost, and terminate netcat after 2 seconds.)

Solution 15 - Android

This scrips can help you to kill All emulators at once:

  1. Filter emulators (because you can have a mixing on physical and emus)
  2. Kill all emus by ADB id

Disadvantage of this solution: if your emu just "stuck" you can't kill it with adb command and it required process kill. But that's very rare case.

while [ "`adb devices | grep -Eoh \"emulator-\d{0,4}\" | wc -l | tr -d ' '`" != "0" ]; do 
	echo "Connected emulators:"
	adb devices | grep -Eoh "emulator-\d{0,4}"

	for emulator in $(adb devices | grep -Eoh "emulator-\d{0,4}")
	do
		echo "Killing the emulator: $emulator"
    	adb -s "$emulator" emu kill | true
	done

	sleep 10; 
done
echo "All emus has been killed"

Solution 16 - Android

To Run and stop an emulator, and clear data

From the Virtual tab, you can perform the following operations on an emulator:

  • To run an emulator that uses an AVD, click Launch.
  • To stop a running emulator, click Menu and select Stop.
  • To clear the data for an emulator, select Wipe Data. Or click Menu and select Wipe Data.

Solution 17 - Android

On Windows 10, with Android Studio 2021.1.1 patch 3, the adb -s emulator-5554 emu kill command does not work, adb being not recognized.

But here's the solution using the Tool/Device Manager. Simply select the active emulator and click on x to stop it.

Killing the selected emulator

Solution 18 - Android

On Linux when the process became unresponsive the only way I could terminate the emulator was using the command:

kill -9 `pidof adb`

which finds the process ID of adb and sends a kill -9 signal to it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjsjroboticsView Question on Stackoverflow
Solution 1 - AndroidSergey ShustikovView Answer on Stackoverflow
Solution 2 - AndroiduweView Answer on Stackoverflow
Solution 3 - AndroidPositive NavidView Answer on Stackoverflow
Solution 4 - AndroidPDHideView Answer on Stackoverflow
Solution 5 - AndroidHuiyang ShanView Answer on Stackoverflow
Solution 6 - AndroidHugo yView Answer on Stackoverflow
Solution 7 - AndroidZenadixView Answer on Stackoverflow
Solution 8 - Androiduser3597306View Answer on Stackoverflow
Solution 9 - Androidchrisjhebert1973View Answer on Stackoverflow
Solution 10 - AndroidSimon WartaView Answer on Stackoverflow
Solution 11 - Androidandroid.weaselView Answer on Stackoverflow
Solution 12 - AndroidastyerView Answer on Stackoverflow
Solution 13 - AndroidGopal ChoudharyView Answer on Stackoverflow
Solution 14 - Androidnaveed148View Answer on Stackoverflow
Solution 15 - AndroidVacxeView Answer on Stackoverflow
Solution 16 - AndroidParesh MangukiyaView Answer on Stackoverflow
Solution 17 - AndroidJean-Pierre SchnyderView Answer on Stackoverflow
Solution 18 - AndroidisapirView Answer on Stackoverflow