1

I am trying to schedule a cron job via RStudio on EC2 instance.
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.

My cron looks like this:

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:

Error in R_function : could not find 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.
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:

Check if R is running in RStudio
Cron job for an R script failing
R command for setting working directory to source file location in Rstudio

Help much appreciated !

ML_Enthousiast
  • 1,147
  • 1
  • 15
  • 39
  • maybe try starting the script by printing Sys.info() to check that cron is finding the correct R binary (and library) — ive had smthg like this issue + the problem was cron was finding the wrong version of R – lefft Jun 30 '18 at 17:05
  • When I do sys.info() I do not see any information about R libraries – ML_Enthousiast Jul 01 '18 at 16:38
  • whoops, try `.libPaths()` and `R.version` -- that'll have the relevant info – lefft Jul 02 '18 at 03:03

1 Answers1

5

You misunderstand how cron and shell commands work -- an R file is not executable (unless you make a change to it, see below).

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

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)
  • 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.
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725