Is It Possible to Run Macro at Shutdown/Restart?

Hello,

I’m trying to find a way to force quit an app whenever I shutdown or restart. The reason for this is that the app requires confirmation upon quitting (Unifi Controller) and prevents unattended restarts. I can’t seem to find a KM trigger that will accomplish this. Any ideas?

Thanks!

I see what you want. But I don't think you want a trigger, what you really should want is a macro that does these things, which is easy enough to write. Then you would trigger the macro manually when you want to shut down or restart. I could tell you why, but before I do, would you be satisfied with a macro that does this, or is it an absolute requirement that this be done automatically with no trigger?

The only problem with a manual solution would be software updates - wherein the OS restarts on its own. In that case, it would hang until I intervened. Ideally, this macro would trigger when the system detects a shutdown or restart. Is that even possible without getting deep into the weeds?

I'm trying to prevent the weeds, too.

So you are saying you have to intercept shutdown. Then I have to think more.

Can you tell me if you have knowledge of an admin password.

Can you tell me if you are paranoid about having an admin password in your KM macro?

Can you tell me if you value shortness of the solution or accuracy/reliability of the solution?

KM doesn't have a trigger for shutdown (it also doesn't have a trigger for startup, just for login, which, for many people, is the same thing, but is actually different.) So this isn't a KM question, but a UNIX question. After all, how would KM prevent itself from being shutdown before your other app gets shut down? These things may happen outside the KM sphere of influence.

However UNIX does have a method for executing things at shutdown or startup. And macOS is Unix. Ipso facto, it can be done. Just not with KM.

I've been on courses on how to modify startup and shutdown scripts. But I'm not sure if I ever did it myself outside of those courses. Here's a page that tells you how:

However that appears to be a different way from the way I did it in the past. Maybe it was changed under macOS, or maybe there's more than one way to do it. It looks intimidating, but I think it looks worse than it is. I think our best bet is to just google a website or find a person who has experience. Believe it or not, I think it can all be done with a single command, and that command probably has two words, "killall" and "Unifi". However knowing where to place those two words is the tricky thing.

I sure appreciate your help. That said, I’m thinking I may need to approach this from the root of the issue versus the symptom. In this case, I’m researching running the offending app as a service as some others have done. I Just don’t want to create a bigger headache instead of a small nuisance… :grinning:

I always try my best. Sometimes I fail, and sometimes my answer isn't the best answer. But I always seek creative new approaches.

Well I’m very thankful for your time. I’ll keep researching. In the meantime, have a blessed evening my friend…

1 Like

Although you went a different direction, I'll post a two-action possible solution if your needs were different. I say possible because I didn't test this yet, and I know that this solution has its own pitfalls which we may have to work out. But I know it has potential.

Hi @Ahwman - welcome to the KM forum.

In the past I’ve used a third-party utility called Eventscripts to trigger my KM macros.

It provides triggers for many event types including system shutdown and wake. Maybe you should take a look at it to see if it will do want you want?

Yeah, I have EventScripts installed, unfortunately it only has a shutdown trigger, nothing for a restart. Thanks for the suggestion however…

Have you tried the sleep/wake triggers? Depending on how intrusive it is to restart Unifi Controller, this might be an option. I toggle a Kasa smart plug and Audio Hijack's run state at sleep and wake, without issue.

Also there's this.

@Ahwman, one solution to the updates problem could be to turn off auto-update and make all your restarts macro-triggered. You could even use KM's timer to run the macro nightly, only firing the restart if certain conditions are met eg if the result of running "software update -l" does not include the string "No new software available".

Unfortunately Apple deprecated logout hooks quite a time back. You might be able to spoof similar behaviour with a constantly running agent that used a script to trigger the KM macro on trapping the event (see this example) but I've never tried that and there would appear to be a lot of things that could go wrong (like what if the KM Engine had already quit in response to the shutdown command?).

Hi @Ahwman. You might find this related information useful:

How Do I Get My Mac to Wake From Sleep for a Macro Triggered at a Specific Time - Questions & Suggestions