Search code examples
rmacoscronrscriptstan

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. 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. I add the job to crontab like this:

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. Specifically, I fit some models using rstan, and get an initialization error. (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.

The session info from R is identical whether I run the script in the terminal or as a cron job. 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? 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. Using launchd triggers the same issue. I also tried using CmdStan through cmdstanr, and the same same thing happens: Runs fine until added to a cron job.

Edit 2: The models I thought ran fine in cron, were not actually fine. The results were wrong, until I used the fix explained below.


Solution

  • 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. I copy-pasted the full output to the top of my crontab file. (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.)

    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.