views:

1284

answers:

4

I've seen a few solutions, including watch and simply running a looping (and sleeping) script in the background, but nothing has been ideal.

I have a script that needs to run every 15 seconds, and since cron won't support seconds, I'm left to figuring out something else.

What's the most robust and efficient way to run a script every 15 seconds on unix? The script needs to also run after a reboot.

+16  A: 

I would use cron to run a script every minute, and make that script run your script four times with a 15-second sleep between runs.

(That assumes your script is quick to run - you could adjust the sleep times if not.)

That way, you get all the benefits of cron as well as your 15 second run period.

RichieHindle
*shakes fist* hehe
Aiden Bell
@Aiden: Ha! My nemesis, we meet again!
RichieHindle
If the script is not consistent in how long it takes to run, make four copies of the script. One sleeps 15 seconds before starting, another 30, another 45, and another zero. Then run all four every minute.
bmb
@RichieHindle - Have no fear, I got assassinated for not granulating the minutes into seconds. But i'm watching you :P
Aiden Bell
+1, and +1 for bmb's comment. A good recipe.
Aiden Bell
Awesome. Works like a charm - thanks!
Nick Sergeant
+1  A: 

Won't running this in the background do it?

#!/bin/sh
while [ 1 ]; do
    echo "Hell yeah!"
    sleep 15
done

This is about as efficient as it gets. The important part only gets executed every 15 seconds and the script sleeps the rest of the time (thus not wasting cycles).

scvalex
+12  A: 

If you insist of running your script from cron:

* * * * * /foo/bar/your_script
* * * * * sleep 15; /foo/bar/your_script
* * * * * sleep 30; /foo/bar/your_script
* * * * * sleep 45; /foo/bar/your_script

and replace your script name&path to /foo/bar/your_script

rasjani
this is very clever. +1
TokenMacGuy
A: 

I don't think you need to have any of the complicated solutions. Doesn't cron allow you to configure how to run every few seconds like this (every 15 seconds is 0/15)

0/15 * * * * * /path/to/my/script

kittenlips