Vote Charlie!

Automatically relaunch unresponsive Mac app

Posted at age 29.

The Insync client on my macOS 10.12.5 installation freezes at least daily, which I only notice when I find files are not syncing across my computers. My hacky solution was to write an AppleScript to check if the client is unresponsive and create a launchd agent to start the client any time it is not running or crashes (different than “Not Responding”).

I use Lingon X to manage my automatic launching, so I created these as follows:

Launcher

  • User: me
  • Name: Insync Launcher
  • Run: /Applications/Insync.app/Contents/MacOS/Insync
  • When: Launch again if it crashes

Killer

  • User: me
  • Name: Insync Killer
  • Run: /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal ~/kill_unresponsive_insync
  • When: Scheduled: Every hour on minute 3

The script must have execute permissions (chmod u+x ~/kill_unresponsive_insync) and Terminal.app must be added under System Preferences > Security & Privacy > Privacy > Accessibility. As this is written, Activity Monitor will be closed if you had it open when the script runs. It should be easy to modify to address this.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/osascript
tell application "Activity Monitor" to run
delay 1
tell application "System Events" to tell process "Activity Monitor"
    tell radio button 1 of radio group 1 of group 2 of toolbar 1 of window 1 to click -- CPU View
    tell outline 1 of scroll area 1 of window 1
        set notResponding to rows whose value of first static text contains "Insync (Not Responding)"
        repeat with aProcess in notResponding
            set pid to value of text field 5 of aProcess
            if pid is not "" then do shell script ("kill -9 " & pid)
        end repeat
    end tell
end tell
tell application "Activity Monitor" to quit

Credits: problem, original script idea, security workaround