I've got a launchd task that runs at 2am every day under my user. I've separately programmed power management to wake up my system at 1:59am so the task will run without any issue. However if the system has to wake to run the task, I would like it to shut down again afterward.
For now my solution is to run pmset sleepnow synchronously at the end of the script.
However this means that my system will shut down shortly after 2am, even if I was already actively using it (which can happen).
Is there some way I can conditionally set the system to sleep only if the system had to wake in order to run the task?