MacOS – How to automatically trigger a bash script before the Mac goes to sleep

macos

I am aware of the options suggested in Possible to run scripts on sleep and wake?.

I am ideally looking for a native option that doesn’t require installing a third-party app or binary.

The closest I found is SleepWatcher but it requires installing a third-party binary using brew install sleepwatcher.

Thanks!

Best Answer

Limited Time

There is no built-in way to run a script before sleep on macOS. You must use a third party tool to trigger your bash script.

The time available for your script, after the pending sleep notification but before the sleep, is deliberately limited by macOS. See Fleeting Sleep Trigger for an overview of the problem.