167

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.

0

18 Answers 18

350

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

9
  • 7
    adb kill-server stops adb but does not stop the emulator process. And the emu kill statement always returns emulator not found Commented Nov 23, 2013 at 23:29
  • 7
    neither work on osx using latest available sdks at this date
    – behelit
    Commented May 17, 2016 at 5:50
  • 20
    adb -s emulator-5554 emu kill does shut down the emulator but the command does not terminate and blocks forever :/ Commented Sep 26, 2016 at 8:27
  • 5
    to anyone who was confused by this, you can find the emulator name by using adb devices.
    – nhouser9
    Commented May 21, 2017 at 16:42
  • 1
    Small typo:) where emulator-5554 should be the case:) Commented May 12, 2022 at 8:08
87

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
4
  • 1
    Do not use -9 to kill the emulator. Normal kill suffices. -9 crashes the process and skips all cleanup, which can lead to problems.
    – V.S.
    Commented Dec 16, 2021 at 11:59
  • 7
    Kill alone didn't kill the process for me, kill -9 did.
    – Javad
    Commented Jan 28, 2022 at 12:32
  • running the kill command didn't work for me, maybe I didn't find the good process to kill or maybe I missed to force (-9). Then I opened the macos activity monitor that list the processes and my emulator was at the top. I double click on it and force quit. That finally worked. Commented Mar 4, 2022 at 9:36
  • I too had to use kill -9. Also I've realised that on the emulator settings, setting Boot option to Cold boot worked better in my case. I think there's a bug with Quick boot which causes emulators to get stuck on Starting Up.
    – Ali Kazi
    Commented Apr 11 at 5:33
56

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
1
  • then, make it a bash alias ;)
    – sanjarcode
    Commented Oct 2, 2023 at 17:43
34

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
0
21

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
1
  • 2
    This is what worked very well for me in Windows 11. You just need to specify that this command must be executed in an Administrator command prompt. Thank you very much.
    – ZipGenius
    Commented Oct 27, 2022 at 10:58
15

I can close it with:

adb shell reboot -p
3
  • What does the -p option do? Power off instead of just reboot? Are there docs that this is pulled from? Thanks! Commented Jan 26, 2022 at 13:40
  • 1
    I found out elsewhere that it just stands for "power". Strange syntax but it works well. It seems to be more graceful compared to kill and emu kill. Commented Jan 26, 2022 at 14:56
  • adb -s emulator-5600 shell reboot -p If you want to close a particular emulator. Commented Apr 1 at 9:42
10

to get your devices name try to run this on Android Studio terminal

adb devices

after you get devices name, kill app with this comment

adb -s emulator-5554 emu kill

where

emulator-5554

is your device name

9

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

telnet localhost 5554
kill
2
  • 1
    but before the Telnet feature needs to be activated through Control Panel > Programs and Features > Turn Windows features on or off > Telnet Client Commented Jan 16, 2015 at 11:19
  • kill doesn't seem to be a command in emulators running Android 9, possibly earlier too
    – Vanquish46
    Commented Nov 27, 2018 at 18:05
9

Why not just do

adb reboot bootloader
2
  • 1
    this eventually worked for me, maybe after calling the other above commands
    – CularBytes
    Commented Jul 2, 2015 at 20:42
  • Really! I thought we can not restart the android emulator through 'reboot' command which version you are using? Commented Mar 13, 2016 at 12:25
2

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.

1

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

3
  • 6
    Does not kill or restart any emulator for me. Commented Sep 26, 2016 at 8:40
  • 6
    adb kill-server kills the server, It does not kill emulators. Commented Oct 28, 2016 at 12:05
  • it worked for me without the usb cable connected. Thanks! Commented Dec 17, 2018 at 13:51
1

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)

1

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

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

0

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"
0

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
0

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.

-5

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

1
  • 2
    It just closes that sub-window for me on Mac. The device is still running.
    – Aidin
    Commented May 28, 2022 at 19:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.