1

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?

1 Answer 1

0

You can extend your launchd task and use pmset -g log to get a log of all wake events. I don't have a Mac handy, so cannot check the output of the command, but it has a reason and a timestamp a machine woke up.

You can either assume if a wake event occurred at 01:59 that it was due to power management, or check the wake reason to see if you can derive it from there.
Then you can make the pmset sleepnow command conditional on the (derived) wake reason, preventing a sleep even when you're actively using the machine at 02:00.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.