Macro to Restart Keyboard Maestro if it Crashes

Hi there,

Does anyone have a working macro to restart Keyboard Maestro if it crashes?

When my Keyboard Maestro crashes, the program usually still has a window open but the Keyboard Maestro rectangular icon on the right-hand side top menu of the Mac disappears, so this may need some Javascript/Applescript to detect since it's not equivalent to simply quitting the application.

Would be grateful for any suggestions/advice on how to set this one up — if it hasn't been done already; I think it would be useful to several people. Thank you.

Keyboard Maestro has two main components:

  • Keyboard Maestro Editor (officially named “Keyboard Maestro”): That’s the GUI program you use to create/modify/organize your KM macros.
  • Keyboard Maestro Engine: That’s the background process that actually makes your macros work: It watches for triggers and runs macros.

KM Editor is only needed when you want to change/create macros, KM Engine is supposed to run always in the background.

The little rectangular menu bar icon on the right-hand side indicates that KM Engine is running (not KM Editor). Engine and Editor are pretty independent, though you can launch or quit KM Engine from within KM Editor (File > Launch Engine / Quit Engine).

So, once KM Engine has crashed, it is pretty impossible to relaunch it with the help of a KM macro (since it is not running anymore).

However, if KM Editor has crashed, you could relaunch it with a macro.

Do you mean the Keyboard Maestro.app, or the Keyboard Maestro Engine?

Of course, if the KME crashes, then NO macros can be triggered/run.

If the Keyboard Maestro.app crashes, you can simply relaunch it, like any app.

However, I have to say that I can't remember the Keyboard Maestro.app ever crashing, and I have it running all day long every day. If it is crashing a lot on you, then you need to resolve that problem.

@peternlewis may have some other suggestions, but I'd start with making sure that KM is sending diagnostics back to the developer. See Preferences > General:

image

Curiously enough, after searching this Forum, I found ONE occasion where the KME crashed on me: KM Engine Crash Report

This was in 2017 and only happened once to me, but you might read that thread to see if there is any info that will help you.

Thanks for the replies and the info.

I can confirm that it is my engine that is crashing — not the app. The app hardly (if ever) crashes for me, but the engine can do on one particular laptop after a few hours use.

Would anyone know if it were possible to create a script to re-launch Keyboard Maestro's engine without using Keyboard Maestro's engine to do so? E.g. an independent script of some kind.

With AppleScript:

tell application id "com.stairways.keyboardmaestro.engine" to launch

Make sure you are submitting any crash reports. There is one rare crash I know of related to spotlight info, that I hope to have resolved in the next version, and there was a crash in 10.12/10.13 related to a very low level bug in the OS, resolved in 10.14, but crashes are very rare in Keyboard Maestro (at least crashes that I get reports for), well less than 1 per 1,000 user-days (and most of them are the spotlight bug).

Thank you! That script set me on the right path to make an application that automatically restarts the engine if it crashes.

Hi Peternlewis,

I have a feeling that the crash report might not be sending to you since I have submitted about 5 in the past week.The engine crashes on me on one particular laptop (but not the others) after a few hours of constant use. So it might just be that particular laptop.

Not sure why you haven't been getting the reports since I always clicked “Send”.

Kind regards,
Chris

If you run this as a LaunchAgent job, it will restart KM Engine:

<?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">
<dict>
	<key>KeepAlive</key>
	<true/>
	<key>Label</key>
	<string>kmengine-keepalive.job</string>
	<key>Program</key>
	<string>/Applications/Keyboard Maestro.app/Contents/MacOS/Keyboard Maestro Engine.app/Contents/MacOS/Keyboard Maestro Engine</string>
</dict>
</plist>

kmengine-keepalive.plist.zip (865 Bytes)

You can add/tweak KeepAlive criteria, so that it only restarts after a crash (and not after a normal Quit), but then you have to restart the job after you have quit KM Engine normally. Depends what is more desirable.


You can use LaunchControl to comfortably create/edit/manage your Launch Agents.

1 Like

Thanks for this! I was looking into plists but didn't work out how to make one, so this is helpful.

Just added a note about the LaunchControl app to my post above. Makes work with Launch Agents easier :wink:

+1 +1

Precisely what I was going to suggest. I do this same thing for Dropbox on my server to make sure that it is always running.

LaunchControl is also a fantastic app and I highly recommend it.

If they have been sent, then I probably have them. They are mostly anonymous, so unless I tracked down your forum email address, and it matches the email address you send with the crash reports, I would not be able to tell (and I normally don't look at such things). If it is related to the spotlight crash (which usually has a reference to spotlight in the thread that crashes), then I will endeavor to get it fixed for the next version.

Thank you for your work peternlewis. You really have a great product and I love using Keyboard Maestro.

Hi Tom, your plist works great! Thanks again.

Just had a quick question. When my Keyboard Maestro Engine is unresponsive (i.e. a spinning loading wheel that never ends and it never recovers) the plist doesn't close and restart the engine in that instance. It seems to think it is still running.

Would you perhaps know how to add that functionality into your current plist to “catch” the unresponsiveness of the engine and restart it then as well?

Thanks,
Chris

I fear there is no really quick answer…

Is a Running Application Frozen

(Especially post 4 and post 10.)


PS:

I don’t think it can be done with KeepAlive. And I don’t know if launchd has some built-in methods to determine process states. But if one of the methods from the above linked thread work for you (especially Peter’s suggestions with Apple Events), you could script this (with a kill command if the test is positive) and run the script periodically via launchd. The KeepAlive job would then restart the process automotically.

There is no reliable way to do this.

There is no functionality in launchd to do this.

The right solution would be to figure out why it is happening. I have to say that I can't remember the last time my Keyboard Maestro Engine was in an unresponsive state, so it could be that something you are asking it to do is misbehaving, which is what is causing the Engine to hang. It might be worth investigating and setting some shorter timeouts to macros that might be the culprit.

In the meantime, a method to identify when the Keyboard Maestro Engine is "stuck" could possibly be developed, but the “how” is somewhat dependent on what happens when it is “stuck” and you try to run a macro -- does it just “hang” forever? Or does it timeout? Or something else?

One thought (which is entirely untested and theoretical) is that you could create a very simple macro that does something trivially easy such as output the letter "A", and then run a shell script every minute that runs that macro.

If the macro fails to output the letter “A” then send a:

pkill 'Keyboard Maestro Engine'

which will cause the engine to exit, and then launchd will restart it automatically.¹

The problem, of course, is that if you are in the midst of running a complex macro when the engine quits, the macro will obviously quit too, which could be annoying, at the very least.

But the short version is that there is no built-in / simple way to do this, which surprises me (Activity Monitor clearly knows when there is an “application not responding” as does macOS in general, but there is no way that I know of for a user to get a "yes / no" response to the question “Is this process not responding?” which seems like something that could / should exist, but does not (again, as far as I am aware, and I have looked for it in the past).


¹ a still-better option would be to run sample on the Keyboard Maestro Engine PID when it appears to be hung, and send that to Peter to see if it offers any clues as to what is going wrong with it.

2 Likes

Thank you Tom and tjluoma,

I've made a little Applescript to test out next time my KM engine crashes (it only crashes on one specific computer @tjluoma which is running the same macros as two others which don't crash, so I suspect it's the laptop that can't handle the specific labor-intensive macro I'm running.)

My little Applescript basically asks the KM engine to set a variable to a random number and then kill KM engine if the variable is not that defined random number. So I'm hoping that, in my particular crash scenario, KM engine is unable to set variables and so, therefore, this trick “catches” the crash.

Let me know if you think this is a bad plan (I'm a newbie)
Chris

1 Like

Well, Activity Monitor might clearly know when an application is not responding, but it doesn’t know if it is “badly” hanging or just heavily working.

For example, when I open a huge file in BBEdit (>100MB on my machine), first nothing happens, then I see the beachball, and then, a couple of seconds after the beachball, the process goes red in Activity Monitor (“not responding”). However, two seconds later BBEdit has succesfully opened the document and continues to work normally. (And becomes black again in Activity Monitor.)

If my launchd job had killed and restarted BBEdit when it turned red, I wouldn’t be pleased :wink:

1 Like

When it crashes, the launchd job from above should relaunch it. Does it not?

(I tested it only with Force Quits, since ATM I don’t know how to reliably produce a KM Engine crash.)