frameworkgeek
frameworkgeek

Reputation: 213

Schedule a Rscript crontab everyminute

For some reason my R script will not run with a crontab. I have it for every minute right now for testing, but will change it once it works.

Any ideas?

* * * * * Rscript “/Users/Home/Desktop/David Studios/Scraper/compiler.R”

Also, this was working as just a normal command in Terminal.

Upvotes: 6

Views: 7327

Answers (4)

Michael Dewar
Michael Dewar

Reputation: 3258

I just spent several hours on a similar problem. I'm still not completely sure what was wrong, but the following command finally worked and I don't really care why. Hopefully this can help someone else.

* * * * * /usr/local/bin/Rscript -e 'source("/home/rstudio/git/myproject/inst/please_work.R")'

Upvotes: 1

victor torres
victor torres

Reputation: 41

You might have a problem with the working directories in R.

When you run the script from the terminal you might be in the directory where the files the script needs are, but when the script runs with cron it uses another directory.

Use the setwd() function inside the R script or use absolute paths when accesing files to make sure the script works no matter where it is being used.

Upvotes: 3

Toolbox
Toolbox

Reputation: 2493

Check that you are really running crontab deamon. You should get a number as return, which is the process id for crontab.

pgrep cron

Make sure your R file is execuable:

sudo chmod +x [yourfile.R]

Add the shebang line in your R file:

#!/usr/local/bin/Rscript

Let crontab do the change of directory:

* * * * * cd /Users/Home/Desktop/David Studios/Scraper/ && /usr/local/bin/Rscript compiler.R

Upvotes: 2

Asaph
Asaph

Reputation: 162801

I can see the dreaded smart quotes in your cron entry. This often happens when you copy-paste from word processors. Backspace over those abominations and re-type normal quotes. Change:

* * * * * Rscript “/Users/Home/Desktop/David Studios/Scraper/compiler.R”

to

* * * * * Rscript "/Users/Home/Desktop/David Studios/Scraper/compiler.R"

See the difference? It's subtle and easy to miss.

Update:

I see you've made the above change and it's still not working for you. Verify that Rscript is in the $PATH environment variable for the user that owns this crontab. Alternatively, you can simply specify the fully qualified path to Rscript directly in the cron entry. You can find that quickly on the command line with the following command:

which Rscript

Update #2:

I see by your comments that the fully qualified path to Rscript is /usr/local/bin/Rscript. I'm guessing /usr/local/bin is not in the path for the user who owns this crontab. Try using the fully qualified path, like this:

* * * * * /usr/local/bin/Rscript "/Users/Home/Desktop/David Studios/Scraper/compiler.R"

Upvotes: 10

Related Questions