Cron every 15 seconds

I cannot set the cron time to less than 1 minute even on my dedicated server. I need it to run every 15 seconds, because it calls the betfair api, and the calculations are very time dependent. Please inform.

+4
source share
3 answers

Cron allows you to run faster than once a minute.

What I would do is do 15 second timings inside the script.

  • Run parent script once per minute.
  • Run scripts for children every 15 seconds inside this script before it exists and a new cycle will begin.
+7
source

If you need to call a job every 15 seconds, do not use cron. cron is designed to request work orders much later.

Instead, after the parent finishes the task, sleep for 15 seconds, then call the script child and then exit it. A child script can do its job, sleep for 15 seconds, and then invoke the next script. Rinse, rinse, repeat.

If your server does not have a time limit, you do not even need to create a child script element. Just ever sleep 15 seconds, do your job, sleep 15 seconds, do the following and so on. A sleeping script does not consume a processor, although it consumes RAM. But this is better than cycling your PID host; which could lead to your host becoming troubled.

+11
source

Cron is a task scheduler that is built with a minimum resolution of one minute. If you need more precise resolution, you will either have to find another planning tool or flip it.

One that I used in the past is as follows. You set the required gap and minimum space, and the following script will execute your payload every N seconds. Note that this is not a simplified "wait N seconds between each iterative" scheduler. It actually starts the next iteration N seconds after the start of the previous one, and not after the end.

There is a minimal gap, so if your payload takes more than N seconds, it does not work continuously. If you want it to work continuously in this situation, just set the minimum space to 0.

Code for this:

 #!/usr/bin/bash payload() { if [[ $1 -eq 1 ]] ; then echo "Sleeping for 1 second at $(date +%H:%M:%S)." else if [[ $1 -lt 10 ]] ; then echo "Sleeping for $1 seconds at $(date +%H:%M:%S)." else echo "Sleeping for $1 seconds at $(date +%H:%M:%S)." fi fi sleep $1 echo " Finished at $(date +%H:%M:%S)." echo } gap=10 mingap=3 for i in {1..20} ; do next=$(($(date +%s) + ${gap})) payload ${i} if [[ ${mingap} -ne 0 ]] ; then sleep ${mingap} fi while [[ $(date +%s) -lt ${next} ]] ; do sleep 1 done done 

The payload is not part of the logic, it is just what I have for debugging purposes. You can replace it with whatever you want. In addition, the for loop is also an example. You can replace it with an infinite loop or use the script only (for example) 60 minutes of iterations when a new instance of the script runs cron every hour.

A space follows. You can see that the payload starts every ten seconds (with an odd eleven seconds due to the vagaries of sleep ) until it takes more than seven seconds. At this point, the minimum clearance begins.

 Sleeping for 1 second at 14:36:08. Finished at 14:36:09. Sleeping for 2 seconds at 14:36:18. Finished at 14:36:20. Sleeping for 3 seconds at 14:36:28. Finished at 14:36:31. Sleeping for 4 seconds at 14:36:39. Finished at 14:36:43. Sleeping for 5 seconds at 14:36:49. Finished at 14:36:54. Sleeping for 6 seconds at 14:37:00. Finished at 14:37:06. Sleeping for 7 seconds at 14:37:10. Finished at 14:37:17. Sleeping for 8 seconds at 14:37:20. Finished at 14:37:28. Sleeping for 9 seconds at 14:37:31. Finished at 14:37:41. Sleeping for 10 seconds at 14:37:44. Finished at 14:37:54. Sleeping for 11 seconds at 14:37:57. Finished at 14:38:08. Sleeping for 12 seconds at 14:38:11. Finished at 14:38:23. Sleeping for 13 seconds at 14:38:27. Finished at 14:38:40. Sleeping for 14 seconds at 14:38:43. Finished at 14:38:57. Sleeping for 15 seconds at 14:39:00. Finished at 14:39:15. Sleeping for 16 seconds at 14:39:18. Finished at 14:39:34. Sleeping for 17 seconds at 14:39:38. Finished at 14:39:55. Sleeping for 18 seconds at 14:39:58. Finished at 14:40:16. Sleeping for 19 seconds at 14:40:19. Finished at 14:40:38. Sleeping for 20 seconds at 14:40:41. Finished at 14:41:02. 
+1
source

All Articles