【问题标题】:R packages (e.g. lubridate) not working when called with Rscript as cron jobR 包(例如 lubridate)在使用 Rscript 作为 cron 作业调用时不起作用
【发布时间】:2018-10-16 20:17:00
【问题描述】:

假设我想让我的计算机每分钟计算一次我的年龄,因此我每天运行以下 cron 作业:

*/1 * * * * bash /path/to/birthCalc

birthCalc 本身调用 R 脚本,如下所示:

#!/bin/bash 
Rscript birthCalc.R

现在,如果我将 base R 与这样的birthCalc.R 一起使用,这一切都有效:

birthDay <- as.POSIXct ('1919-04-15', format = '%Y-%m-%d')
age <- floor ((as.numeric (Sys.time ()) - as.numeric (birthDay))/(365.25*60*60*24))

但是,如果我尝试通过 lubridate 库使用以下脚本,它就不再起作用了。

library ('lubridate')
birthDay <- as.POSIXct ('1919-04-15', format = '%Y-%m-%d')
age <- floor (lubridate::time_length (Sys.time () - birthDay, "years"))

如果我在 cron 作业中运行 installed.packages (),则会列出 lubridate,因此我假设 cron 作业应该能够找到库。我在其他库中也遇到过这个问题,但选择 lubridate 作为示例。

脚本仅在作为 cron 作业运行时才会失败。我可以从 bash 终端或在 R 中以交互方式运行它。我检查了 cron 是否使用相同的 Rscript 和 R,以及包是否可以访问。我错过了什么?

我正在跑步:

R 版本 3.4.4 (2018-03-15)

平台:x86_64-pc-linux-gnu(64 位)和 Ubuntu 16.04.5 LTS

lubridate_1.7.4

非常感谢, 总时间

【问题讨论】:

    标签: r cron lubridate


    【解决方案1】:

    我找到了解决方案。它似乎与权限有关。我最初以 root 身份创建了 cron 作业,并尝试为用户创建它。使用crontab -u user -e,而不是sudo crontab -e 解决了这个问题。尽管库安装在可访问的目录中,但有人知道为什么这是一个问题吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-23
      • 2021-04-06
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2016-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多