This may not be the most elegant solution, but here's what I came up with.
Since the problem is that when you kill the monkey script, the process on the Android device does not clean up properly, so when you try to reconnect, you get a problem with the channel. You can kill the -9 monkey process on the device itself and you will no longer run into it.
I have two scenarios that work around the problem here. This is python:
3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice 4 from time import sleep 5 import sys 6 import signal 7 import subprocess 8 9 device = None 10 11 def execute(): 12 device = MonkeyRunner.waitForConnection() 13
And the killer monkey script:
#!/bin/bash var=$(adb shell ps | grep monkey | awk '{print $2}') echo $var adb shell kill -9 $var
When I ctrl + c monkey python script, it calls killmonkey, which kills the process on the connected Android device.
This specifically works only if only one device is connected, as it does not indicate the device identifier. Not very elegant, but I find it effective.
Brian source share