crontab to wake osx from sleep

18

11

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?

Nippysaurus

Posted 2009-07-29T04:49:17.170

Reputation: 1 223

Answers

29

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.

jtb

Posted 2009-07-29T04:49:17.170

Reputation: 2 115

1Great answer, but can you wake it when the laptop is closed? – User – 2016-12-07T02:04:18.463

The link is broken. – retracile – 2011-11-26T22:59:43.103

4

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.

Lri

Posted 2009-07-29T04:49:17.170

Reputation: 34 501

3

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

Richard Hoskins

Posted 2009-07-29T04:49:17.170

Reputation: 10 260

2

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

Bruce McLeod

Posted 2009-07-29T04:49:17.170

Reputation: 5 490

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.

John T

Posted 2009-07-29T04:49:17.170

Reputation: 149 037