Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I would like to run a shell script daily, but not necessarily at a specific time. The computer the script runs on is switched on at irregular times, and no assumptions can be made when that will be.

For that, I would like to get launchd to behave a bit like anacron: It only needs to run the job once a day, at any convenient time.

Most posts I have seen here or on other sites describe things like "running every day at 05:00", which is not helpful for me if the machine is off at that time...

How do I write a launchd plist specifying that behaviour?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

Launchd will run your jobs next time the mac wakes from sleep. So if you shut down your mac, this won't work. You will need to put your mac to sleep to get your daily script to run when the mac wakes if the last time it should have run was during the sleep duration.

man launchd.plist:

If the system is asleep, the job will be started 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.

Or in other words: You don't have to do anything specific, it's the default behavior anyway.

If you also want to run the command once after reboot, add

<Key>RunAtLoad</Key>
<true/>

to your launchd plist.

share|improve this answer
    
Thanks for the reply, but I need an approach that will also work if the machine has been turned off. –  jstarek Aug 13 '11 at 23:28
    
Use the RunAtLaunch option, see answer –  patrix Aug 14 '11 at 13:07
2  
Ah, sorry, my mistake. However, at least on Snow Leopard, it's RunAtLoad, not RunAtLaunch -- or am I making the next mistake here? :-) –  jstarek Aug 18 '11 at 16:31
    
I stand corrected, answer is fixed. –  patrix Aug 18 '11 at 20:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.