6

I am trying to schedule my R script using cron, but it is not working. It seems R can not find packages in cron. Anyone can help me? Thanks.

The following is my bash script

# source  my profile
. /home/winie/.profile
# script.R will load packages
R CMD BATCH /home/script.R 

4
  • 1
    This is a cross post from r-help -1 Apr 12, 2012 at 4:33
  • 1
    @TylerRinker, would it have been okay if OP had cross-linked in both places? I generally try to use r-help, but sometimes people do not answer and then I've added it here with a bounty.
    – Eric Fail
    Sep 18, 2012 at 2:57
  • 3
    It's ok to post in two places just link each post on different sites or help lists back to the other so someone isn't solving a problem that's already been solved. Sep 18, 2012 at 3:08
  • @TylerRinker, good point. Thanks for responding!
    – Eric Fail
    Sep 18, 2012 at 3:20

1 Answer 1

13

Consider these tips

  1. Use Rscript (or littler) rather than R CMD BATCH

  2. Make sure the cron job is running as you

  3. Make sure the script runs by itself

  4. Test it a few times in verbose mode

My box is running the somewhat visible CRANberries via a cronjob calling an R script (which I execute via littler but Rscript should work just as well). For this, the entry in /etc/crontab on my Ubuntu server is

# every few hours, run cranberries
16 */3 * * *    edd     cd /home/edd/cranberries && ./cranberries.r

so every sixteen minutes past every third hour, a shell command is being run with my id. It changes into the working directory, and call the R script (which has executable modes etc).

Looking at this, I could actually just run the script and have setwd() command in it....

2
  • This is intriguing...can you post the R line of code to use to setwd(). I mean, how to pass the parameter from command line?
    – Gabriele B
    Oct 30, 2015 at 18:39
  • It's a fixed parameter here: setwd("/home/edd/cranberries"); ...rest of script.... But look into doctopt for command-line parameter processing... Oct 30, 2015 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.