简体   繁体   English

在RStudio中运行cron作业

[英]Run cron job within RStudio

I am trying to schedule a cron job via RStudio on EC2 instance. 我正在尝试通过EC2实例上的RStudio安排cron作业。
When I use a basic script with very basic functions it works fine. 当我使用具有非常基本功能的基本脚本时,它可以正常工作。 But when I want to schedule a script that contains R functions from specfic R packages, the scripts fails. 但是,当我要调度包含特定R包中的R函数的脚本时,脚本将失败。

My cron looks like this: 我的cron看起来像这样:

MAILTO="myemail@adress.com"
36 * * * * /home/user1/Test_for_Cron.R

After the script is launched, I receive an email from Cron Daemon specifying something like: 启动脚本后,我收到Cron Daemon的电子邮件,内容如下:

Error in R_function : could not find function R_function R_function中的错误:找不到函数R_function
Execution halted 执行停止

From what I found, it seems I have to specify that I want to run the script within RStudio or that I want to "source" it. 从发现的内容来看,似乎必须指定要在RStudio中运行脚本,还是要“获取”该脚本。
I have the feeling the answer might be in the combination of these 3 posts below but I am not finding the solution after testing out some suggestions: 我觉得答案可能是下面这3个帖子的组合,但是在测试了一些建议后我找不到解决方案:

Check if R is running in RStudio 检查R是否在RStudio中运行
Cron job for an R script failing R脚本的Cron作业失败
R command for setting working directory to source file location in Rstudio R命令,用于将工作目录设置为Rstudio中的源文件位置

Help much appreciated ! 帮助非常感谢!

You misunderstand how cron and shell commands work -- an R file is not executable (unless you make a change to it, see below). 您会误解cron和shell命令的工作方式-R文件不可执行(除非您对其进行更改,请参见下文)。

But if you just prefix it with Rscript you can 1. test it in your shell until it works 2. have it executed by cron so do 但是,如果只给它加上Rscript前缀,则可以1.在您的shell中对其进行测试,直到它起作用为止2.由cron执行它,这样

36 * * * * Rscript /home/user1/Test_for_Cron.R

Edit: You can make it executable if you do two things: 编辑:如果您做两件事,则可以使其可执行:

  • add a shebang line #!/usr/bin/Rscript (or whatever the path to Rscript is) 添加一个shebang#!/usr/bin/Rscript (或任何指向Rscript的路径)
  • change the mode to executable via chmod 755 Test_for_Cron.R which sets read/write/execute for you (owner) and read/execute for group and others. 通过chmod 755 Test_for_Cron.R将模式更改为可执行文件,它为您(所有者)设置读/写/执行,为组和其他设置读/执行。

Thank you Dirk. 谢谢德克。 Yes I did add already #!/usr/bin/Rscript to my script. 是的,我确实已经#!/usr/bin/Rscript脚本中添加了#!/usr/bin/Rscript The problem actually came from the simple fact that some libraries were not loaded in my script, causing errors and stopping the script. 问题实际上来自一个简单的事实,即我的脚本中未加载某些库,从而导致错误并停止了该脚本。 Now that all the libraries are loaded, it is working fine! 现在,所有库都已加载,一切正常!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM