I'm trying to make an automatic launch of the R script. I need to start the creaking every new five-minute done like this

while (TRUE) { Sys.sleep(300) print(Sys.time()) ------------- Основной код ------------- } 

but due to the fact that the main code runs for quite a long time, it turns out an unacceptably strong time offset

 [1] "2018-07-23 09:37:02 CEST" [1] "2018-07-23 09:42:37 CEST" [1] "2018-07-23 09:48:48 CEST" [1] "2018-07-23 09:55:05 CEST" 

I think to do this: in the while loop, constantly check the time of Sys.time () and when the beginning of a new five-minute session begins, 9:45:01, then 9:50:01, and so on. run the script

But what should the code look like by the definition of the five minutes? who knows? Or are there alternative ways? Thanks in advance for your reply

  • Get the execution time of the "main code", better in seconds. 5 * 60 = 300, and make sleep for 300-time_sec (checking that time_sec <300) - nick_n_a
  • The fact is that the code is always executed for a different count. of time, but definitely less than 5 minutes, for me the best way to implement it is to run on every new five-minute type like this: 9:45:01, then 9:50:01, and so on. - mr.T
  • I understand your idea ... you can do while ( (min % 5) != 0 ) sleep(1); - nick_n_a
  • Sorry, but you can write the code completely as it should be, I'm just not a very programmer, but what you wrote does not start (( - mr.T

1 answer 1

More efficient use of task scheduler. This functionality for Windows is implemented in the taskscheduleR package.

 library(taskscheduleR) script_path <- file.path("bin", "script.R") # Создаём задание taskscheduler_create(taskname = "myfancyscript_5min", rscript = script_path, schedule = "MINUTE", modifier = 5) # Получаем список заданий tasks <- taskscheduler_ls() str(tasks) # Удаляем задание # taskscheduler_delete(taskname = "myfancyscript") 

For Linux, there is a package with similar functionality - cronR .