32

I have a crontab which will launch a certain program early in the morning (after I have gone to sleep) and close it again later in the morning (before I wake up). But my iMac will not execute the crontab script while it is asleep.

I see there is some preferences to wake the machine up and make it sleep again at a specified time, but is there a way to do this using the crontab?

5 Answers 5

45

cron doesn't execute while the computer is asleep, so there's no way for you to have a crontab entry to wake the computer. That said you can schedule the computer to wake just a minute or two before your cron task in Preferences >> Energy Saver >> Schedule.

It's also worth pointing out that since Tiger, Apple has moved most scheduled jobs from cron and scripts like init.rc to the launchd process. It provides more detailed (but cumbersome, I think) means of control (through several plist files). It will also automatically run tasks missed because the computer was sleeping when it awakens. Take a look at Apple's Scheduling Timed Jobs page.

2
  • The link is broken.
    – retracile
    Nov 26, 2011 at 22:59
  • 3
    Great answer, but can you wake it when the laptop is closed?
    – User
    Dec 7, 2016 at 2:04
10

Another way to schedule waking up from sleep is to use pmset:

sudo pmset repeat wakeorpoweron MTWRFSU 03:45:00

launchd cannot wake a computer from sleep, but it does run jobs scheduled during sleep after waking up. If others search for how to do that, you can for example save this plist as ~/Library/LaunchAgents/some.label.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>some.label</string>
  <key>ProgramArguments</key>
  <array>
    <string>say</string>
    <string>a</string>
  </array>
  <key>StartCalendarInterval</key>
  <dict>
    <key>Hour</key>
    <integer>23</integer>
    <key>Minute</key>
    <integer>59</integer>
  </dict>
</dict>
</plist>

Then run launchctl load ~/Library/LaunchAgents/some.label.plist.

4

You can schedule your Macintosh to wake up in the Energy Saver preference panel. See the Schedule button.

3

You might want to look into Anacron

Anacron runs the periodic daily, weekly and monthly tasks on your Mac even if the machine (a laptop, for example) spends much of its time asleep or switched-off. Anacron silently checks when you reboot and every sixty minutes while the computer is running to see if the various periodic scripts are overdue, and runs them if necessary. The advantage of Anacron over many other solutions to this issue is that it runs as a proper Unix background process, requires no user intervention, and uses the regular periodic scripts.

Another applicaiton that might do the trick is Awaken

1
1

cron will not run while OSX is in sleep mode.

You may want to look into cronwake (must run through google translate as the site is japanese I believe) and anacron, or you can just use the Energy Saver application to schedule a wake time to run the script, then put it back to sleep.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .