[android] Android Stop Emulator from Command Line

This question is identical to How to shut down Android emulator via command line.

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.

This question is related to android android-emulator

The answer is


Use adb kill-server. It should helps. or

adb -s emulator-5554 emu kill, where emulator-5554 is the emulator name.

For Ubuntu users I found a good command to stop all running emulators (Thanks to @uwe)

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

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 {}"

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

telnet localhost 5554
kill

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

Why not just do

adb reboot bootloader

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


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.


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

I hope this will help you!


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.)


I can close it with:

adb shell reboot -p

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

FOR MAC:

  1. Run:
ps -ax | grep emulator 

which gives you a wider 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 first PID (in the above example, it's 6617).

  3. Force kill that process:

kill -9 PID

In my case, the command is:

kill -9 6617
  1. Usually, killing the first process in enough to stop the emulator, but if that doesn't work, try killing other processes as well.

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)