knowads knowads - 1 year ago 38
R Question

R- Get script to run every X minutes regardless of execution time.

I have a function of variable execution time, but is going to be under 20 seconds for most use cases. I want a script to execute the function every 10 minutes regardless of how long the function actually takes (therefore cannot use the sleep command or any 'wait' command)

Answer Source

in your loop:

p1 <- Sys.time()
yourFunct()
theDelay <- 600-as.numeric(difftime(Sys.time(),p1,unit="secs"))
Sys.sleep(max(0, theDelay)

or add relevant handling when the function takes > 10 minutes, if you don't want it to immediately run again