ML_Enthousiast
ML_Enthousiast

Reputation: 1269

Run cron job within RStudio

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="[email protected]"
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 !

Upvotes: 1

Views: 2611

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368191

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.

Upvotes: 5

Related Questions