2

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)

knowads
  • 705
  • 2
  • 7
  • 24
  • See [this](http://stackoverflow.com/questions/2793389/scheduling-r-script) or [this](http://stackoverflow.com/questions/10116411/schedule-r-script-using-cron) – Sumedh Aug 12 '16 at 17:29
  • Not on a Windows OS or have an MS tools available. Ideally I'd like this to work with red Hat – knowads Aug 12 '16 at 17:32

1 Answers1

3

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

hedgedandlevered
  • 2,314
  • 2
  • 25
  • 54
  • 1
    Thanks. It should never take longer than 10 minutes unless there is massive user error. – knowads Aug 12 '16 at 17:35
  • 1
    @knowads Then you should probably do if (theDelay <= 0) stop("Massive User Error!!!") or something like that. – AlexR Aug 12 '16 at 22:11