Logout trigger?

I know that I can trigger a macro on login, but is there any way to trigger a macro on logout?

You could set up a macro with the desired actions, then just add a Log Out action at the end:

[demo] Anything + Logout.kmmacros (1.6 KB)

Then, instead of logging out, launch that macro.

Thank for the suggestion @Tom. If that is my only choice, I suppose I can use that, although it is not preferable. I have created some macros that monitor my child’s Internet data usage amount and want to send an email automatically when he logs out. Training him to use a macro to log out could be a problematic…

OK, I see the problem.

As far as I know it is not possible to tie a KM macro to the logout. I guess, this would either abort the logout process or the logout would stop the macro. But maybe @peternlewis has an idea…

Some other things that come to mind:

  • If you could train your child to use the keyboard shortcut (⇧⌘Q) to log out you could hijack the shortcut with the KM macro
  • Use a periodic trigger
  • Make the macro trigger when a certain application quits (e.g. the web browser)
  • Trigger the macro after login (so it would send the email report from the day before)

There is no current logout trigger.

It might be possible to write an applet that used the info at this stack overflow post to detect when you are quitting and then send the email.

Presumably you don’t really care how the logout is happening, just that the Mac is logging out/shutting down/restarting/whatever, so just detecting the quit and sending the email should be sufficient.

I know you want to do this with KM, but I had to solve this same problem when Apple dropped support for Logout Items a few years back.

This is done fairly reliably using Launch Agents at either User or System (root) levels and a pretty simple shell script which I’m happy to share. Launch Agents (daemons) are pretty easy to write as an XML plist file by hand using online tutorials, or bone simple with a GUI manager like Lingon X or Lingon 3 (Mac App Store; User-level only).

My script also relies on email from the shell, which requires some minor mods to your system postfix files and works best with gmail and other single-factor login POP providers.

I’m afraid there’s no easy way to use Apple Mail via AppleScript to do the email; the Logout request will (gracefully) kill any user processes and generally prevents any new ones from spawning. For that same reason, 4/5 tries to send email via shell immediately upon Logout will be delayed by Postfix until the next User login, so if that delay in reporting is unacceptable, there are still more workarounds using System-level Agents.

I use one agent at the User level to write to a log file with date and time upon logout, and a second at root level that watches that log file, and upon change, reads its recorded date and time and sends it in an email. Easy peasy.

The same can be done for Login, of course, and does not rely on KM which can be disabled.

1 Like

Thanks to everyone for the great suggestions. I think I am going to start off with the suggestion by @Tom to initiate the logout via a macro. Hopefully that will be enough.

@Tom I just wanted to let you know that your suggestion seems to be working so far. My son is dutifully logging out via the macro! :+1:


Great to hear, thanks for the feedback. Your son seems to be very dutiful :innocent::wink:


Is it possible that you could help me with this? I have a similar situation where I want to execute a script at logoff and my searches came up here.


@phillipsmn, so sorry; just now seeing this; do you still require logout daemons to perform an action? Please describe.

And now it’s my turn to apologize for the lateness of response! Yes, I have a couple of situations where I’d like to execute a script at logoff. One of which is to send an email or a notification via boxcar2 that this is happening (which a quick python script can do). I’d love to know how to use the logout daemon to do so.