Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shut down Android emulator via command line

I am unable to stop the emulator from command prompt gracefully.

I am using Linux Ubuntu v10.04 (64-bit) and Android v2.3 (API 9 - Gingerbread).

I started emulator using its snapshot. Now my concern is to shut down the running instance of Emulator gracefully. I have tried with kill -9 (process Id for emulator running) which shuts down the emulator but next time it does not start as its snapshot got corrupted. Please help me to avoid forceful shutdown of the an emulator.

Any idea how to fix it?

like image 968
Sam Avatar asked May 06 '11 13:05

Sam


People also ask

How do I force close an Android Emulator?

To stop a running emulator, click Menu and select Stop.

How do I run an emulator from command prompt?

If you have to use command line for creating your AVD, you can call android create avd -n <name> -t <targetID> where targetID is the API level you need. If you can use GUI, just type in android avd and it will launch the manager, where you can do the same.


1 Answers

Please don't use kill -9 indiscriminately, it's a very bad habit.

The correct command is

 $ adb emu kill 

Or I should better say it was the correct command until some recent adb changes. It seems somebody forgot to add the authentication to it.

In the latest (as of June 2016) the latest adb version is

$ adb version Android Debug Bridge version 1.0.36 Revision 0a04cdc4a62f-android 

and when you try

$ adb emu kill 

nothing happens, and this is why

... connect(3, {sa_family=AF_INET, sin_port=htons(5554),  sin_addr=inet_addr("127.0.0.1")}, 16) = 0 write(3, "kill\nquit\n", 10)            = 10 read(3, "\377\373\1", 8192)             = 3 read(3, "\377\373\3\377\373\0\377\375\0", 8192) = 9 read(3, "Android Console: Authentication required\r\nAndroid Console: type 'auth <auth_token>' to authenticate\r\nAndroid Console: you can find your <auth_token> in \r\n'/home/diego/.emulator_console_auth_token'\r\nOK\r\n", 8192) = 202 read(3, "k\33[K", 8192)                 = 4 read(3, "\33[Dki\33[K", 8192)           = 8 read(3, "\33[D\33[Dkil\33[K\33[D\33[D\33[Dkill\33[K", 8192) = 28 read(3, "\r\nKO: unknown command, try 'help'\r\n", 8192) = 35 read(3, "q\33[K\33[Dqu\33[K", 8192)     = 12 read(3, "\33[D\33[Dqui\33[K\33[D\33[D\33[Dquit\33[K", 8192) = 28 read(3, "\r\n", 8192)                   = 2 read(3, "", 8192)                       = 0 close(3)                                = 0 exit_group(0)                           = ? +++ exited with 0 +++ 

Then we need another solution.

If the previous command does not work (as some users reported for Windows) you can try (in the next command 5554 is the port used by the emulator).

Copy the content of the token file (~/.emulator_console_auth_token) to the clipboard so you can paste it during your telnet session:

$ telnet localhost 5554  Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Android Console: Authentication required Android Console: type 'auth <auth_token>' to authenticate Android Console: you can find your <auth_token> in  '/home/user/.emulator_console_auth_token' OK auth <YOUR_TOKEN_HERE> Android Console: type 'help' for a list of commands OK Android console command help:      help|h|?         print a list of commands     crash            crash the emulator instance     kill             kill the emulator instance     quit|exit        quit control session     redir            manage port redirections     power            power related commands     event            simulate hardware events     avd              control virtual device execution     finger           manage emulator fingerprint     geo              Geo-location commands     sms              SMS related commands     cdma             CDMA related commands     gsm              GSM related commands     rotate           rotate the screen by 90 degrees  try 'help <command>' for command-specific help OK 

Then, you can just enter kill at the command prompt

kill OK: killing emulator, bye bye Connection closed by foreign host. 

and the emulator will exit.

But wait, there should be a better way. And in fact there is!

This gist provides an automated solution using expect instead of having to cut and past the authentication token every time.

Hope you find it useful.

like image 74
Diego Torres Milano Avatar answered Sep 28 '22 08:09

Diego Torres Milano