[英]Is there a way to trigger a cron job to run based on existence of file and not specific time?
我正在尝试使用 cron r 或 R 中的任务调度程序来每天运行基于每天更新的 a.CSV 文件的脚本。 一件事是 CSV 文件没有更新的具体时间(假设在 4 月 20 日它在下午 3 点更新,但在 4 月 21 日它在下午 2:30 更新,在 4 月 22 日它在中午 12 点)。 主要触发因素不是一天中的时间,而是文件的日常存在。 有没有办法可以使用 R 插件中的任何一个来运行它? 我在工作中使用服务器,所以我没有使用 windows 任务调度程序,因为 R 不在我的机器上。
与其每天运行 cron 作业,不如每 5 分钟(或某个合理的时间间隔)运行一次,并跟踪它处理文件的时间。 例如,
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")
}
我可能会稍微扩展needswork
以添加通知问题,例如
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 严格来说是一个基于时间的调度程序。
话虽如此,有一个解决方法。
import os.path
if os.path.isfile("/tmp/myfile.csv"):
# File exists
# Do something
else:
# File does not exist
pass
Python 脚本只是一个示例。 随意使用您最喜欢的脚本语言
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.