Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have created a cronjob that runs every 60 seconds which is not frequent enough for me. It is a simple command: wget localhost How can I run this commenad every 30, 16 seconds on my mac?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Run your command using launchd instead. Use the property list files in ~/Library/LaunchAgents/ and /System/Library/LaunchAgents/ as inspiration. Documentation on developer.apple.com.

To specify a launch interval of e.g. 30 seconds, use the following:

<key>StartInterval</key>
<integer>30</integer>
share|improve this answer
    
A good tool for generating launchd.plist files is Lingon. –  NReilingh Feb 5 '11 at 17:08
2  
This answer got me curious about how the runtime of the program affects when it's launched, so I did some tests: as long the program takes less than 30 seconds to run, it will be started every 30 seconds (e.g. if it's started at 1:00:00 and runs until 1:00:17, the next run will start at 1:00:30 not 1:00:47). OTOH if it takes over 30 seconds, the next run will wait until this run finishes (e.g. if it runs until 1:00:34, the next run starts at 1:00:34). –  Gordon Davisson Feb 5 '11 at 18:57

Write a bash script which runs every minute. Put the wget in a loop, or simply consecutive calls, with sleep 30 between each call.

share|improve this answer

You could try the following:

while true
do
<program>
sleep <time>
done

Replace and as appropriate.

Edit: When you're done needing to run ever seconds, Control+c will kill it.

share|improve this answer
    
Assuming <program> takes longer than 0 seconds, this will be off a bit at first, accumulating offset time the longer it runs. –  Daniel Beck Feb 6 '11 at 16:10
    
True. This is a direct opposite behavior to launchd, which will miss a run if the program runs longer than the time interim. –  alesplin Feb 6 '11 at 21:23
    
Your program misses these launches as well, as the program is still running. Only Doug's solution might get them, I don't know enough about cron to be sure though. –  Daniel Beck Feb 6 '11 at 23:11
    
That's what I'm saying. Mine will run, sleep for <time> and run again, regardless of how long it takes to run, unless the program backgrounds itself. launchd will run the program, and if it runs longer than <time> it will wait until it finishes. Cron (as far as I understand) will run the program every <time> (if <time> fits cron's granularity) and this may result in multiple instances running at once if it runs longer than <time>. So he's got 3 options for his desired behavior. Not knowing what the OP wants, I figured to post an alternative that may be simpler than cron or launchd. –  alesplin Feb 7 '11 at 6: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.