Android 从命令行停止模拟器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20155376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:23:09  来源:igfitidea点击:

Android Stop Emulator from Command Line

androidandroid-emulator

提问by jsjrobotics

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

此问题与如何通过命令行关闭 Android 模拟器相同。

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

但是,在尝试第一个答案中建议的解决方案后,adb emu kill对我来说并没有成功。

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 avdand executes emulatorwith the -no-windowattribute. It then compiles the test project, connects to the emulator using adb, installs the project and executes my tests. This all works fine.

我正在为一个 android 应用程序自动化单元测试。我的 bash 脚本在无头机器上运行。它使用该属性创建一个 android 设备android create avdemulator使用该-no-window属性执行。然后编译测试项目,使用 连接到模拟器adb,安装项目并执行我的测试。这一切正常。

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

现在我需要终止模拟器进程,就像引用的帖子一样,我只能使用kill -9.

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

Google 教程从命令行管理 AVD仅提到如何在 GUI 环境中停止模拟器。

Any help is appreciated.

任何帮助表示赞赏。

回答by Sergey Shustikov

Use adb kill-server. It should helps. or

使用adb kill-server. 它应该有帮助。或者

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

adb -s emulator-5554 emu kill,emulator-5554模拟器名称在哪里。

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

对于 Ubuntu用户,我发现了一个很好的命令来停止所有正在运行的模拟器(感谢@uwe

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

回答by uwe

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

回答by Zenadix

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

另一个答案对我不起作用(在 Windows 7 上)。但这有效:

telnet localhost 5554
kill

回答by Zenadix

Why not just do

为什么不做

adb reboot bootloader

回答by Navid Vafaei

FOR MAC:

对于 MAC:

  1. Run:
  1. 跑:
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:

  1. 第一列(左)是您要查找的进程 ID (PID)。

  2. 找到第一个 PID(在上面的例子中,它是 6617)。

  3. 强制终止该进程:

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.
  1. 通常,杀死第一个进程足以停止模拟器,但如果这不起作用,也尝试杀死其他进程。

回答by Hugo y

I can close it with:

我可以关闭它:

adb shell reboot -p

回答by Huiyang Shan

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

由于未知原因,它在我的 CI 服务器或台式机上不起作用。我认为在 Windows 上可以杀死 qemu 的进程,就像

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

回答by astyer

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

如果你不想知道你的设备的序列名adb -s emulator-5554 emu kill,那么你可以只用adb -e emu kill杀死一个模拟器。如果您同时运行多个模拟器,这不会杀死任何东西,但它对于启动和停止单个模拟器进行测试的自动化很有用。

回答by Simon Warta

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

没有一个解决方案对我有用。我不得不采用 telnet 方式,包括身份验证:

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

完整脚本可以从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)

更新:看起来这仍然不能可靠地关闭控制台和 ADB 端口(例如 5554,5555)

回答by android.weasel

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

我使用这个单行,为了可读性分成几行:

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