How to run an R script in crontab

倖福魔咒の 提交于 2019-12-20 03:27:08

问题


Using crontab -e I've tried:

* * * * *  Rscript /home/.../file.r
* * * * * /usr/lib/R/bin/Rscript /home/.../file.r
* * * * * /usr/bin/Rscript /home/.../file.r
* * * * * /home/.../foo.sh

where foo.sh contains:

sudo R CMD BATCH file.r

Just running $ ./foo.sh works. $ R CMD BATCH file.r works.

Nothing I've tried in crontab works. Any ideas?


回答1:


You'll need to have the full path in your foo.sh:

sudo R CMD BATCH /home/.../file.r

I should also add that the first version worked for me, although I set it to a specific time rather than * * * * *




回答2:


For what it is worth here is CRANberries entry which has worked (multiple times) every day for 5+ years:

# every few hours, run cranberries
11 */2 * * *    edd     ~/cranberries/cranberries.r

Of note here are

  1. The time specification, here 11 mins past the hour every two hours
  2. The user
  3. The complete path, using 'globbing' to expand ~ to $HOME for edd

and after that you just have to make sure the script is actually executable. Whether you use Rscript, littler or R BATCH CMD does not matter.




回答3:


in my user level crontab I run a R script to collect tweets from various sources.

I used: crontab -u myusername -e added this to the end of my crontab file

0 0 * * * /home/bob/Documents/SWtweets/tweetbash.sh

I ran it as a user vs root, don't what difference it makes, but it only worked for me when I ran it at user level.

below is what I put in my tweetbash.sh shell script, which tells the bash program to run the R script located at the path indicated.

#! /bin/bash R CMD BATCH /home/bob/Documents/SWtweets/tweets.R

did chmod +x tweetbash.sh

it works for my purposes.

Bob



来源:https://stackoverflow.com/questions/12014837/how-to-run-an-r-script-in-crontab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!