Mac Executing code on, or after a certain time/date?

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
267
0
I'm looking for a way to execute an app, or a block of code at or after a certain time. I've used cron and crontab in the terminal to arrange certain programs to run weekly at the certain date/time. However, if the computer is off at the time specified, the program doesn't run at all. What I need to do in this new application is have a program run at a certain date/time if the computer is on, or start running when the computer is turned on for the first time after that date/time has passed. Any ideas?
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
Have you tried launchd ?

From the launchd.plist man page :

StartCalendarInterval <dictionary of integers or array of dictionary of integers>
This optional key causes the job to be started every calendar interval as speci-
fied. Missing arguments are considered to be wildcard. The semantics are much like
crontab(5). Unlike cron which skips job invocations when the computer is asleep,
launchd will start the job the next time the computer wakes up. If multiple
intervals transpire before the computer is woken, those events will be coalesced
into one event upon wake from sleep.

Minute <integer>
The minute on which this job will be run.

Hour <integer>
The hour on which this job will be run.

Day <integer>
The day on which this job will be run.

Weekday <integer>
The weekday on which this job will be run (0 and 7 are Sunday).

Month <integer>
The month on which this job will be run.
Info : http://www.devdaily.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs
https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Articles/LaunchOnDemandDaemons.html
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
I'll toss "at" out there for you to test. Its job list should survive reboot, so it may run those jobs the first chance it gets. No experience with this, just something else to check out.

-Lee
 

Mac_Max

macrumors 6502
Mar 8, 2004
404
0
Or use cron/launchd to run a bash script that logs if the app has been run within x period of time and if not, runs it. Then set the script as a startup item as well.
 

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
267
0
Thanks Lee, I'll take a look into the "at" command.

Max, do items set as startup items run when you awake from sleep, when you log in, or just when you turn on the computer?
 

mrichmon

macrumors 6502a
Jun 17, 2003
873
2
What I need to do in this new application is have a program run at a certain date/time if the computer is on, or start running when the computer is turned on for the first time after that date/time has passed. Any ideas?
"anacron" is a command that behaves like cron with the exception that it will run commands that were scheduled while the machine was sleeping or powered off. The commands scheduled to run when the machine is sleeping or powered off will be run when the machine is next awake.

anacron can be installed using fink or mac ports.