37

I'm using the monkey tool to run a test of my Android application. For example, I might do a run like the following:

adb shell monkey -p com.myapp -v 10000

However, if I change my mind and need to cancel the test, there doesn't seem to be a way to do so that doesn't involve waiting multiple minutes for the damned monkey to finish most or all of its run.

Killing the adb shell process on my mac doesn't solve the problem. Killing the com.myapp process on my phone using ddms doesn't work. Unplugging my phone doesn't work.

How do I cancel the monkey madness?

emmby
  • 99,783
  • 65
  • 191
  • 249

6 Answers6

57

You can kill the monkey process just doing this:

$ adb shell ps | awk '/com\.android\.commands\.monkey/ { system("adb shell kill " $2) }'
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
16

[Nitpick] You're confusing monkeyrunner with monkey.

The monkeyrunner tool is not related to the UI/Application Exerciser Monkey, also known as the monkey tool. The monkey tool runs in an adb shell directly on the device or emulator and generates pseudo-random streams of user and system events. In comparison, the monkeyrunner tool controls devices and emulators from a workstation by sending specific commands and events from an API.

[/Nitpick]

On my Android 2.2 device when I start monkey, I see a process started in DDMS by the name "?" (just a question mark). When I killed that process, the monkey madness stopped.

Dheeraj Vepakomma
  • 26,870
  • 17
  • 81
  • 104
9
adb shell
ps | grep monkey
kill process_id
DynamicMind
  • 4,240
  • 1
  • 26
  • 43
  • 3
    the process_id referred to in the kill statement is the first number returned by the "ps | grep monkey" command - that would be necessary to know – Groovee60 Apr 07 '16 at 01:35
3
adb shell kill $(adb shell pgrep monkey)

kudo to @deadfish

Henry
  • 942
  • 3
  • 11
  • 21
1

For what it's worth, I use Android Studio 3.1.4 on a Mac in 2018 and I had to alter the accepted answer like so:

./adb shell ps | awk '/com\.android\.commands\.monkey/ { system("./adb shell kill " $2) }'

Hope that help prevent some hair-pulling and pencil snapping out there!

Also... when it comes to the monkey, always be sure to pin your app!!! Otherwise you might accidentally send all your selfies to a random email in China like I did. ¯\_(ツ)_/¯

Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
0

Kill the monkey by shell will cause a small problem, the IActivityController in ActivityTaskManagerService will not be set to null, which it should. And the ActivityManager.isUserAMonkey() still return true.

If monkey stop automatically, it will reset the Controller properly:

Monkey.java{
    private int run(String[] args) {
        ...
        try {
            mAm.setActivityController(null, true);
            mNetworkMonitor.unregister(mAm);
        }
        ...
    }
}
  • Could you add a little more detail? Where should this be run? All other answers suggest shell commands while this is a Java snippet. – darken Dec 15 '21 at 08:06
  • When the monkey start, ActivityTaskManagerService init ActivityController, set mControllerIsAMonkey true. – Long Chen Dec 24 '21 at 03:39
  • Obviously, when monkey stop will set null and false, except monkey killed.Maybe is a bug ╮(─▽─)╭. I am using shell commands too.Bug I modify the ActivityTaskManagerService.isControllerAMonkey() to fix it. – Long Chen Dec 24 '21 at 05:36