150

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.

Community
  • 1
  • 1
jsjrobotics
  • 1,705
  • 2
  • 12
  • 10

18 Answers18

331

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

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • 6
    adb kill-server stops adb but does not stop the emulator process. And the emu kill statement always returns emulator not found – jsjrobotics Nov 23 '13 at 23:29
  • emulator not found? Never believe of that. if you start emulator - you have emulator. Didn't? Try to kil emulator of another port. Numbers of ports consists in link above. – Sergey Shustikov Nov 24 '13 at 00:10
  • Your comment on verifying the port I am using was correct. Can't believe I over read that part in the document you posted. Had been assuming that the argument for -s was the name of the emulator. – jsjrobotics Nov 25 '13 at 17:46
  • 7
    neither work on osx using latest available sdks at this date – behelit May 17 '16 at 05:50
  • 16
    `adb -s emulator-5554 emu kill` does shut down the emulator but the command does not terminate and blocks forever :/ – Simon Warta Sep 26 '16 at 08:27
  • 5
    to anyone who was confused by this, you can find the emulator name by using `adb devices`. – nhouser9 May 21 '17 at 16:42
  • @funkybro my phone is currently running 6.0.1 and I can confirm that ADB still exists (I use my phone to test programs made by the android SDK and NDK almost daily). – Sergey Shustikov Jun 24 '17 at 13:49
  • on Windows 10, the "adb -s emulator-5554 emu kill" command blocks on the command line. (Same issue exists in both windows cmd line and gitbash shell) – Oke Uwechue Jul 14 '21 at 19:45
  • 1
    Small typo:) where `emulator-5554` should be the case:) – Daniel Danielecki May 12 '22 at 08:08
73

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
Positive Navid
  • 2,481
  • 2
  • 27
  • 41
  • 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. Dec 16 '21 at 11:59
  • 4
    `Kill` alone didn't kill the process for me, `kill -9` did. – Javad Jan 28 '22 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. – francoiscabrol Mar 04 '22 at 09:36
51

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
uwe
  • 3,931
  • 1
  • 27
  • 19
28

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
PDHide
  • 18,113
  • 2
  • 31
  • 46
18

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
Huiyang Shan
  • 431
  • 6
  • 8
  • 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 Oct 27 '22 at 10:58
15

I can close it with:

adb shell reboot -p
Hugo y
  • 1,421
  • 10
  • 20
  • What does the `-p` option do? Power off instead of just reboot? Are there docs that this is pulled from? Thanks! – Joshua Pinter Jan 26 '22 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`. – Joshua Pinter Jan 26 '22 at 14:56
9

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

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

Why not just do

adb reboot bootloader
6

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

Vanya Rachel
  • 1,329
  • 1
  • 18
  • 20
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.

astyer
  • 21
  • 1
1

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

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)

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
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 {}"
android.weasel
  • 3,343
  • 1
  • 30
  • 41
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
Dharman
  • 30,962
  • 25
  • 85
  • 135
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.)

naveed148
  • 548
  • 5
  • 8
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"
Vacxe
  • 91
  • 5
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.

isapir
  • 21,295
  • 13
  • 115
  • 116
-4

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

Jean-Pierre Schnyder
  • 1,572
  • 1
  • 15
  • 24