Your best option is to use cron
or Apple's launchd
. Since you want whatever it is to be executed at set intervals without a delay after waking up from sleep this is what I recommend.
Cron Method
To setup a new cron job you would open up Terminal and edit it with the time information and script you are wanting to execute (eg. every 7 minutes):
$ crontab -e
*/7 * * * * /usr/bin/python /path/to/myscript.py
Here's a quick breakdown of the meaning:
* * * * * command to execute
│ │ │ │ │
│ │ │ │ └─── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └──────── month (1 - 12)
│ │ └───────────── day of month (1 - 31)
│ └────────────────── hour (0 - 23)
└─────────────────────── min (0 - 59)
To list jobs you have set in your crontab:
$ crontab -l
Timed Jobs Using launchd
Apple's recommendation is not to use crontab
, rather launchd
. Basically this entails creating a preference list with the information about your task and what time to run it, etc.
$ cd $HOME/Library/LaunchAgents
$ nano com.username.mytask.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>com.username.mytask</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/myscript.sh</string>
</array>
<key>StartInterval</key>
<integer>7</integer>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
In nano press Control + O followed by Control + X to save.
$ chmod +x /path/to/myscript.sh
$ launchctl load com.username.mytask.plist
$ launchctl start com.username.mytask.plist
The following would make your script executable, and then load and start the launch agent.
To stop and unload:
$ launchctl stop com.username.mytask.plist
$ launchctl unload com.username.mytask.plist
More information:
↳ Scheduling Timed Jobs
↳ Creating a launchd Property List File
Effects of Sleeping and Powering Off
If the system is turned off or asleep, cron jobs do not execute; they
will not run until the next designated time occurs.
If you schedule a launchd job by setting the StartCalendarInterval key
and the computer is asleep when the job should have run, your job will
run when the computer wakes up. However, if the machine is off when
the job should have run, the job does not execute until the next
designated time occurs.
All other launchd jobs are skipped when the computer is turned off or
asleep; they will not run until the next designated time occurs.
Consequently, if the computer is always off at the job’s scheduled
time, both cron jobs and launchd jobs never run. For example, if you
always turn your computer off at night, a job scheduled to run at 1
A.M. will never be run.