Keep in mind that
cron can’t run a script (or other app) at intervals smaller than one minute.
So if you want to simulate sending data at say, 10 second invervals, you’ll need to get creative with
Here’s an example of a simple Python script that can run at the interval you choose.
As written, it prints the text foo and the time, every 5 seconds.
import time, traceback
def every(delay, task):
next_time = time.time() + delay
time.sleep(max(0, next_time - time.time()))
# in production code you might want to have this instead of course:
# logger.exception("Problem while executing repetitive task.")
# skip tasks if we are behind schedule:
next_time += (time.time() - next_time) // delay * delay + delay
More info from the script author:
Exception handling: As far as possible on this level, exceptions are handled properly, i.e. get logged for debugging purposes without aborting the program.
No chaining: The common chain-like implementation (for scheduling the next event) you find in many answers is brittle in the sense that if anything goes wrong within the scheduling mechanism,
threading.Timer, etc), it terminates the chain. No further execution will happen even if the cause of the problem is fixed. A simple loop and waiting with a simple
sleep() is much more robust in comparison.
No drift: This solution keeps track of the time it’s supposed to run at. There is no drift depending on the called code’s execution time (as in many other solutions).
Skipping: This solution will skip tasks if an execution took too long (e. g. do X every five seconds, but X took 6 seconds). This is the standard cron behavior (and for a good reason). Many other solutions then simply execute the task several times in a row without any delay. For most cases (e. g. cleanup tasks) this is not desired. If it is desired, simply use
next_time += delay instead.
There are other examples on the page at the link above.
To be fair, you can use cron to run a job at intervals smaller than one minute. Here’s how it can be done:
It’s something of a hack, though…