I am trying to use cron r or task scheduler in R to run a script daily based off a.CSV file that gets updated everyday. The one thing is there is no specific time of the day the CSV file gets updated (let's say on 4/20 it got updated at 3PM but at 4/21 it got updated at 2:30PM and at 4/22 it got updated at 12PM). The main trigger is not time of day but daily existence of file. Is there a way I can run this using either of the R addins? I use a server at work so I am not using windows task scheduler since R is not on my machine.
Instead of running the cron job every day, run it every 5 minutes (or some reasonable interval), and keep track of when it processed the file. For example,
needswork <- function(filename, expr, updated = paste0(filename, ".seen")) {
if (!file.exists(filename)) return(FALSE)
if (!file.exists(updated)) return(TRUE)
return(file.info(updated)$mtime < file.info(filename)$mtime)
}
donework <- function(filename, expr, updated = paste0(filename, ".seen")) {
writeLines(character(0), updated)
}
if (needswork("/path/to/mainfile.csv")) {
# process the file here
# ...
# update
donework("/path/to/mainfile.csv")
}
I might extend the needswork
a little to add notification problems, such as
needswork <- function(filename, expr, updated = paste0(filename, ".seen")) {
if (!file.exists(filename)) return(FALSE)
if (difftime(Sys.time(), file.info(filename)$mtime, units="secs") > 60*60*24) {
some_notify_function()
# perhaps something like
msg <- paste("The file", sQuote(filename), "has not been updated since",
file.info(filename$mtime))
RPushbullet::pbPost("note", title = "No recent updates", body = msg)
}
if (!file.exists(updated)) return(TRUE)
return(file.info(updated)$mtime < file.info(filename)$mtime)
}
Cron is strictly a time based scheduler.
Having said that, there is a work around.
import os.path
if os.path.isfile("/tmp/myfile.csv"):
# File exists
# Do something
else:
# File does not exist
pass
Python script is just an example. Feel free to use your fav scripting language
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.