[英]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.