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 want to run a script every 15 minutes between 12:00 AM and 4:00 AM. Is there a way to do that with launchd?

I know you can specify and array of dictionaries using StartCalendarInterval to accomplish this, but I would like to easily be able to change the interval if I so desire. I was thinking something along the lines of combining StartCalendarInterval with StartInterval, but I don't know how I would specify a stopping time.

The other alternative is to check the time in my script I'm running, but once again, I think I'd rather use launchd to accomplish the task in case I want to add more scripts.

I'm pretty sure this can be done with cron as well (We could put it here for reference if anyone knows how), but I want to stick with launchd since Apple seems to be phasing out cron.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You can print the StartCalendarInterval dictionaries with something like this: for h in {0..4}; do for m in {0,15,30,45}; do echo "<dict><key>Hour</key><integer>$h</integer><key>Minute</key><integer>$m</integer><key>Second</key><integer>0</integer></dict>"; done; done.

You could also run the script every 15 minutes but exit it depending on the time:

h=$(date +%-H)
m=$(date +%-M)
[[ $h -gt 4 || $h -eq 4 && $m -ne 0 ]] && exit

Or run EDITOR=nano crontab -e and add a line like */15 0-4 * * * ~/bin/script.

share|improve this answer
    
+1 for this because vi was set as my default editor and kept failing to save the crontab file. Error was crontab: "/usr/bin/vi" exited with status 1 –  brandwaffle Jan 22 '13 at 1:51

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.