How can I automatically trigger a bash script before my Mac goes to sleep?
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.