简体   繁体   English

Rscript & rstan 作为 cron 作业运行时的不同行为

[英]Different behavior when Rscript & rstan is run as a cron job

I try to run an R script at regular intervals to update a webpage.我尝试定期运行 R 脚本来更新网页。 The script runs fine when called from the terminal like this:当从终端调用时,脚本运行良好,如下所示:

/usr/local/bin/Rscript /Users/me/path/myscript.R

However, if I try running it as a cron job, I get an error.但是,如果我尝试将其作为 cron 作业运行,则会出现错误。 I add the job to crontab like this:我像这样将作业添加到 crontab:

46 10 * * * /usr/local/bin/Rscript '/Users/me/path/myscript.R'  >> '/Users/me/path/mylog.log' 2>&1

The script does run in R, but aborts due to an error.该脚本确实在 R 中运行,但由于错误而中止。 Specifically, I fit some models using rstan, and get an initialization error.具体来说,我使用 rstan 拟合了一些模型,并得到了初始化错误。 (The error only applies to some models, while others still run fine.) The initialization values are valid by definition, but do not seem to be used properly. (该错误仅适用于某些模型,而其他模型仍然运行良好。)初始化值根据定义是有效的,但似乎没有正确使用。 It is like rstan is doing math differently (and wrong) when it is run through cron.就像 rstan 在通过 cron 运行时所做的数学运算不同(并且是错误的)。

The session info from R is identical whether I run the script in the terminal or as a cron job.无论我在终端中运行脚本还是作为 cron 作业运行,来自 R 的 session 信息都是相同的。 My question is what else might still differ depending on how the script is run.我的问题是根据脚本的运行方式,还有什么可能会有所不同。 Could rstan be using a different version of C++ when run as a cron job?作为 cron 作业运行时,rstan 是否可以使用不同版本的 C++? Are there other paths I may need to set to get this to work correctly?我可能需要设置其他路径才能使其正常工作吗?

Update: The script also works if I run it using R CMD BATCH in terminal, but not if I use R CMD BATCH in a cron job. Update: The script also works if I run it using R CMD BATCH in terminal, but not if I use R CMD BATCH in a cron job. Using launchd triggers the same issue.使用launchd会触发同样的问题。 I also tried using CmdStan through cmdstanr , and the same same thing happens: Runs fine until added to a cron job.我还尝试通过CmdStan使用cmdstanr ,并且发生了同样的事情:在添加到 cron 作业之前运行良好。

Edit 2: The models I thought ran fine in cron, were not actually fine.编辑 2:我认为在 cron 中运行良好的模型实际上并不好。 The results were wrong, until I used the fix explained below.结果是错误的,直到我使用下面解释的修复程序。

It looks like I finally managed to fix this, and I'm posting my solution here for anyone who encounters the same problem.看起来我终于设法解决了这个问题,我在这里为遇到同样问题的任何人发布我的解决方案。

I ran env in terminal to see my current user environment.我在终端中运行env以查看我当前的用户环境。 I copy-pasted the full output to the top of my crontab file.我将完整的 output 复制粘贴到我的 crontab 文件的顶部。 (Simply adding the PATH variable was not sufficient. I suppose it was SHELL or perhaps both PATH and SHELL that did the trick, but I haven't explored this further.) (仅仅添加 PATH 变量是不够的。我想它是 SHELL 或者可能是 PATH 和 SHELL 做到了这一点,但我还没有进一步探索。)

To edit my user's crontab, I ran crontab -e , then pressed i to edit the file, pasted everything from env at the top of the file, stopped editing by pressing ctrl + c , and quit by typing :wq and hitting enter.要编辑我的用户的 crontab,我运行了crontab -e ,然后按i编辑文件,将env中的所有内容粘贴到文件顶部,按ctrl + c停止编辑,然后键入:wq并按 Enter 退出。

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

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