1

I am trying to schedule a Crontab job for, say, Monday at 12:59, even when my Mac is sleeping. To achieve this I use pmset repeat wake M 12:58:55 to wake up my Mac 5 seconds before the job.

The first problem I encounter is that it is awakened (at the login screen) at 12:58:00 instead of 12:58:55. Since the login screen time before is goes to sleep again is 30 seconds, by 12:59:00 my Mac is already sleeping again, so my job never runs.

Is there a way to start the job at the desired time and/or extend the time before my Mac goes back to sleep at the login screen? Currently is set to 30 seconds.

R. C.
  • 11
  • What does your crontab look like? Also, is your Mac set to sleep after only 1 min? If so, then you’re not giving the cron job enough time to kick off. – Allan Jan 18 '23 at 13:37
  • 59 12 * * 1 bash my_script.sh I don't know how to set the sleep time for the login screen (before I enter my user and password). – R. C. Jan 18 '23 at 14:55
  • 1
    See Graham’s answer below; it’s excellent. You are cutting things super close, plus things don’t happen as instantaneously as you think they do. Consider using launchd instead of cron. – Allan Jan 18 '23 at 15:37

1 Answers1

1

Hardware Limitations

This is a hardware constraint. Scheduled wake up and power on times are not accurate to the second. Typically they happen within thirty seconds of the scheduled time.

With successive versions of macOS, Apple's engineers have become increasingly aggressive with the return to sleep period after scheduled wakes. If no user activity, or a power assertion, is detected, expect your Mac to return to sleep after ten to fifteen seconds.

There is currently no formal documentation from Apple about this behaviour; it has been discovered through experience.

Perform on Wake

Ideally, try to enqueue your task to perform when the Mac wakes from sleep. A script could check the time and determine if the wake has occurred within an acceptable time span and then perform the desired task.

Graham Miln
  • 43,776
  • Can you be more specific please? If wake up times are accurate +- 30sec it becomes pretty much impossible to run a crontab. If you want to launch a script with crontab at 12:59:00 how should I perform the waking? – R. C. Jan 18 '23 at 14:57
  • 1
    You are right. Coordinating a cron job with a scheduled wake up is unlikely to be reliable. See https://apple.stackexchange.com/search?q=script+on+wake for methods to run a script on wake. – Graham Miln Jan 18 '23 at 16:03