Skip to content

Instantly share code, notes, and snippets.

@reitermarkus
Last active October 10, 2015 17:34
Show Gist options
  • Save reitermarkus/1cbe18fec5172353ea25 to your computer and use it in GitHub Desktop.
Save reitermarkus/1cbe18fec5172353ea25 to your computer and use it in GitHub Desktop.
OS X LaunchDaemon to automatically wake or boot your Mac every 15 minutes in case of a power failure – using pmset.
#!/bin/sh
launchd_name='com.apple.AutoWake'
launchd_plist=/Library/LaunchDaemons/"$launchd_name".plist
sudo dd of=$launchd_plist <<'EOF' &>/dev/null
<plist version="1.0"><dict><key>ProgramArguments</key><array><string>/bin/bash</string><string>-c</string><string>weekdays=MTWRFSU; eventtype=wakeorpoweron; sudo pmset repeat $eventtype $weekdays 05:30:00; for i in {0..23}; do seconds=`date +%s`; seconds=$(($seconds+(3600*$i))); hour=`date -r $seconds +'%m/%d/%y %H'`; minute=`date -r $seconds +%M`; for min in 00 15 30 45; do if [[ $i != 0 ]] || ([[ $i == 0 ]] &amp;&amp; [[ $minute -lt $min ]]); then if ! sudo pmset -g sched | grep $eventtype | grep -q "$hour:$min:00"; then sudo pmset schedule $eventtype "$hour:$min:00"; fi; fi; done; done</string></array></dict></plist>
EOF
sudo plutil -convert xml1 $launchd_plist
sudo defaults write $launchd_plist Label -string "$launchd_name"
sudo defaults write $launchd_plist RunAtLoad -bool true
sudo defaults write $launchd_plist KeepAlive -bool false
sudo defaults write $launchd_plist StartInterval -int 300
sudo chmod 644 $launchd_plist
#!/bin/sh
weekdays=MTWRFSU
eventtype=wakeorpoweron
sudo pmset repeat $eventtype $weekdays '05:30:00'
for i in {0..23}; do
seconds=`date +%s`
seconds=$(($seconds+(3600*$i)))
hour=`date -r $seconds +'%m/%d/%y %H'`
minute=`date -r $seconds +%M`
for min in 00 15 30 45; do
if [[ $i != 0 ]] || ([[ $i == 0 ]] && [[ $minute -lt $min ]]); then
if ! sudo pmset -g sched | grep -q "$eventtype at $hour:$min:00 by '$weekdays'"; then
sudo pmset schedule $eventtype "$hour:$min:00" $weekdays
fi
fi
done
done
<?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>com.apple.AutoWake</string>
<key>KeepAlive</key>
<false/>
<key>RunAtLoad</key>
<true/>
<key>StartInterval</key>
<integer>300</integer>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>-c</string>
<string>weekdays=MTWRFSU; eventtype=wakeorpoweron; sudo pmset repeat $eventtype $weekdays &apos;05:30:00&apos;; for i in {0..23}; do seconds=`date +%s`; seconds=$(($seconds+(3600*$i))); hour=`date -r $seconds +&apos;%m/%d/%y %H&apos;`; minute=`date -r $seconds +%M`; for min in 00 15 30 45; do if [[ $i != 0 ]] || ([[ $i == 0 ]] &amp;&amp; [[ $minute -lt $min ]]); then if ! sudo pmset -g sched | grep -q &quot;$eventtype at $hour:$min:00 by &apos;$weekdays&apos;&quot;; then sudo pmset schedule $eventtype &quot;$hour:$min:00&quot; $weekdays; fi; fi; done; done</string>
</array>
</dict>
</plist>
@reitermarkus
Copy link
Author

Put com.apple.AutoWake.plist into /Library/LaunchDaemons/ and run these commands:

sudo chmod 644 /Library/LaunchDaemons/com.apple.AutoWake.plist
sudo launchctl load /Library/LaunchDaemons/com.apple.AutoWake.plist

or

curl -sL https://gist.githubusercontent.com/reitermarkus/1cbe18fec5172353ea25/raw/autowake-install.sh | sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment