Skip to content

Instantly share code, notes, and snippets.

@jjthrash
Last active December 17, 2015 19:49
Show Gist options
  • Select an option

  • Save jjthrash/5662739 to your computer and use it in GitHub Desktop.

Select an option

Save jjthrash/5662739 to your computer and use it in GitHub Desktop.
How to automate the process of properly killing iOS simulator processes

The problem: XCode leaves behind zombie processes

When you click the "stop" button to kill a simulator process in XCode, it leaves behind a zombie process. You'll see something like this:

diannao:options-test (09:04) $ ps -fu jjthrash
…
  501  1503   228   0 Fri09AM ??         0:00.00 (myIOSApp)
…

This may seem like a merely aesthetic annoyance, but the reality is these zombie processes take up allowed process count, and you'll eventually run out of processes on your system, leading to weird behavior and ultimately requiring you to reboot.

The solution: Detach, then kill

Thanks to this Stack Overflow Post answer: Xcode leaving zombie processes after running iOS tests/simulator I have been able to work out a scheme to minimize this problem.

The basic flow is as follows:

  1. In XCode, type ^⌘y to pause the process
  2. In the debugger session, type process detach
  3. In the simulator, simulate going to home screen: ⌘h
  4. In the simulator, kill the app: ⌘h twice to pull up processes, tap and hold on process icon, tap (-) icon.

The problem is, this is annoying, and annoying things are unreliable due to human nature.

I have automated steps 1–3 using Keyboard Maestro (script attached), but you can use whatever automation tool you like. In theory I could automate the whole thing, but gave up when I ended up playing with sleep statements.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Activate</key>
<string>Normal</string>
<key>IsActive</key>
<true/>
<key>Macros</key>
<array>
<dict>
<key>Actions</key>
<array>
<dict>
<key>AllWindows</key>
<true/>
<key>AlreadyActivatedActionType</key>
<string>Normal</string>
<key>Application</key>
<dict>
<key>BundleIdentifier</key>
<string>com.apple.dt.Xcode</string>
<key>Name</key>
<string>Xcode</string>
<key>NewFile</key>
<string>/Applications/Xcode.app</string>
</dict>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>MacroActionType</key>
<string>ActivateApplication</string>
<key>ReopenWindows</key>
<false/>
<key>TimeOutAbortsMacro</key>
<true/>
</dict>
<dict>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>KeyCode</key>
<integer>16</integer>
<key>MacroActionType</key>
<string>SimulateKeystroke</string>
<key>Modifiers</key>
<integer>4352</integer>
<key>ReleaseAll</key>
<false/>
</dict>
<dict>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>MacroActionType</key>
<string>Pause</string>
<key>Time</key>
<string>1</string>
<key>TimeOutAbortsMacro</key>
<true/>
</dict>
<dict>
<key>Action</key>
<string>MoveAndClick</string>
<key>Button</key>
<integer>0</integer>
<key>ClickCount</key>
<integer>1</integer>
<key>DisplayMatches</key>
<false/>
<key>DragHorizontalPosition</key>
<string>0</string>
<key>DragVerticalPosition</key>
<string>0</string>
<key>Fuzz</key>
<integer>15</integer>
<key>HorizontalPositionExpression</key>
<string>0</string>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>MacroActionType</key>
<string>MouseMoveAndClick</string>
<key>Modifiers</key>
<integer>0</integer>
<key>MouseDrag</key>
<string>None</string>
<key>Relative</key>
<string>Window</string>
<key>RelativeCorner</key>
<string>Center</string>
<key>RestoreMouseLocation</key>
<false/>
<key>VerticalPositionExpression</key>
<string>450</string>
</dict>
<dict>
<key>Action</key>
<string>ByTyping</string>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>MacroActionType</key>
<string>InsertText</string>
<key>Paste</key>
<false/>
<key>Text</key>
<string>process detach</string>
</dict>
<dict>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>KeyCode</key>
<integer>36</integer>
<key>MacroActionType</key>
<string>SimulateKeystroke</string>
<key>Modifiers</key>
<integer>0</integer>
<key>ReleaseAll</key>
<false/>
</dict>
<dict>
<key>AllWindows</key>
<true/>
<key>AlreadyActivatedActionType</key>
<string>Normal</string>
<key>Application</key>
<dict>
<key>BundleIdentifier</key>
<string>com.apple.iphonesimulator</string>
<key>Name</key>
<string>iOS Simulator</string>
<key>NewFile</key>
<string>/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app</string>
</dict>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>MacroActionType</key>
<string>ActivateApplication</string>
<key>ReopenWindows</key>
<false/>
<key>TimeOutAbortsMacro</key>
<true/>
</dict>
<dict>
<key>IsActive</key>
<true/>
<key>IsDisclosed</key>
<true/>
<key>KeyCode</key>
<integer>4</integer>
<key>MacroActionType</key>
<string>SimulateKeystroke</string>
<key>Modifiers</key>
<integer>768</integer>
<key>ReleaseAll</key>
<false/>
</dict>
</array>
<key>IsActive</key>
<true/>
<key>ModificationDate</key>
<real>391374239.23181999</real>
<key>Name</key>
<string>Detach from debugger process</string>
<key>Triggers</key>
<array>
<dict>
<key>FireType</key>
<string>Pressed</string>
<key>KeyCode</key>
<integer>40</integer>
<key>MacroTriggerType</key>
<string>HotKey</string>
<key>Modifiers</key>
<integer>4864</integer>
</dict>
</array>
<key>UID</key>
<string>6735F179-D66B-4C28-9D0A-8CD691335CE5</string>
</dict>
</array>
<key>Name</key>
<string>Jimmy's</string>
<key>UID</key>
<string>DAB55CC4-4FA7-48F8-B9ED-ACE40413DDA8</string>
</dict>
</array>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment