Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
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?
 
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
 
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
 
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.
 
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?
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.