CRON not executing R script file - r

I am trying to automate running of a script (test.R) in R stored on a desktop folder (cron_test). I am using CRON in my mac terminal. The example R script generates random numbers, plots them, and saves them to a PDF:
setwd("~/Desktop/cron_test")
pdf("example_plot.pdf", width = 8, height = 8)
plot(rnorm(100))
dev.off()
My CRON code attempts to call this every minute, but nothing appears in the folder:
* * * * * Rscript /Users/username/Desktop/cron_test/test.R
Nothing is being generated. I have tried the following:
The file runs fine if I call it from the terminal, suggesting there is not an issue with the code:
Rscript /Users/username/Desktop/cron_test/test.R
I have also ensured (and double-checked) that the permissions iof the file has executable permission, as per this proposed answer to a similar issue:
chmod 777 /Users/username/Desktop/cron_test/test.R
I have also tried the other proposed solution from this answer, which is to create a shell file that calls the R script. This doesn't work, either.
I am receiving mail to state that the CRON task has run, but no output is appearing in my cron_test folder.

Does the mail say
/bin/sh: Rscript: command not found
?
If so, try using the full path to Rscript. You can find this from the terminal with
$ which Rscript
The reason for this is that the environment you get in a cron session is much smaller compared to that in an interactive terminal. You can examine this by comparing the outputs of
$ /usr/bin/env > env-interactive
and the cron job
* * * * * /usr/bin/env > env-cron
We can see these are quite different by counting the number of lines in each file.
$ wc -l env-{interactive,cron}
20 env-interactive
8 env-cron
In particular the shell executable search path ($PATH) in the cron job is much shorter
$ grep PATH env-{interactive,cron}
env-interactive:PATH=/Users/hpcchris/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/opt/X11/bin:/Applications/Wireshark.app/Contents/MacOS:/Users/hpcchris/bin
env-cron:PATH=/usr/bin:/bin
(Using the full path /usr/local/bin/Rscript worked for me with R on my macos High Sierra laptop and produced the plot.)

Related

Crontab Redhat 8: Crontab will not run an R script that works perfectly otherwise

I'm currently trying to run a R file every minute through crontab. The R file take in a file from the previous directory and inputs into a sub-directory. If I just ./script.R the command will run fine, read and output all the data 100% accurately. However, in crontab it will not even run. I have tried a sample crontab to test if it was my crontab not working first, I wrote
* * * * * /bin/echo "foobar" >> /home/a/t/test.log
This command would correctly send foobar to the log.
However when I did
* * * * * /home/a/t/script.R >> /home/a/t/test.log
I would get no results within the test.log, nor would I get results in the subdirectory it should store in after being ran. I also did the script another way to see if the * were causing issues
*/1 * * * * /home/a/t/script.R >> /home/a/t/test.log
This also did not give any results.
After some research what I can understand is that there is likely an issue with paths. So to try to counteract that I did a cd command along with the R command as such:
*/1 * * * * cd /home/a/t/ && /home/a/t/script.R >> /home/a/t/test.log
Still, however, this has given 0 output. If I type
grep CRON /var/log/cron
I see cron trying to run the lines every minute, but again no output found anywhere, I dont think it is running and I can't understand why not.
Also in terms of path for the R script itself, within the R script I have the first line written something like
#!/bin/env Rscript -S --vanilla
which allows me to ./script.R and run it after a chmod as well.
This exact crontab commands also worked for me on my other laptops ubtunu vm, which is causing more confusion.
The command for running Rscript should be
Rscript path/to/Rscript.R
Please test the command by running it in your terminal. Once it run perfectly fine then just take the whole command and put it in crontab.

Close and reopen + run an R script from itself on Mac

I would like to write an R script that can close and reopen + run itself.
This is needed for an API query that I am trying to make and which seems to require me going through these steps once every hour to be able to make additional requests. I tried to use the source() function - and simply run my script from itself every hour- but with this the API keeps rejecting additional requests; it seems that actually closing and opening the program is necessary.
I also tried to use the system() command - as described here to actually open R and execute the script - but I was not able to figure out how to implement this in a Mac environment.
Would you have any suggestions on how to do this?
The usual way to run a script every x amount of time on a Unix system is a cronjob. I'm not familiar with macOS, but apparently it works just as on Linux.
Open the job list to edit it (you can of course use another editor instead of nano)
env EDITOR=nano crontab -e
Add a line/job to the file. This runs any command line command. You can use Rscript to run an R script.
0 * * * * Rscript "/path/to/your/script.R"
Exit and safe. This script should now run every hour.
If you want to change the timing check out crontab.guru. Check out this answer, if the cronjob reports Rscript to be missing.

Pandoc while using R markdown with a cron command

I'm trying to create a cron command that will use R markdown to create a new html page at specified intervals. I've discovered this is a pandoc issue.
I get the following error message when I log my cron command
Error: pandoc version 1.12.3 or higher is required and was not found
(see the help page ?rmarkdown::pandoc_available). Execution halted
Is there a simple bit of code I can add to the .Rmd file to point it to pandoc when executing the cron command?
Preserving the original post. That is below this paragraph.
Everything I want to do is a a file titled test_doc.Rmd.
When I run the following command on the command line, it works successfully:
RScript -e "library(rmarkdown); render(\"/path/test_doc.Rmd\")"
However, when I run that in the crontab, I'm having no success. I'm running a version of this:
25 10 * * * RScript -e "library(rmarkdown); render(\"/path/test_doc.Rmd\")"
I'm baffled. I don't believe it's a filepath issue, since I have other R scripts (not rmarkdown) running in the crontab and working. I am on Mac OS X 10.10.5
The same has happened to me, and I found the answer in a related post regarding your error message (which I haven't even seen):
Error: pandoc version 1.12.3 or higher is required and was not found (see the help page ?rmarkdown::pandoc_available). Execution halted
You have to specify the RSTUDIO_PANDOC environment variable before rendering like so:
Rscript -e 'Sys.setenv(RSTUDIO_PANDOC="/usr/lib/rstudio/bin/pandoc"); rmarkdown::render("test_doc.Rmd")'
This should solve your cronjob issue. It worked for me.
I am assuming that most Linux+RStudio users have pandoc installed in this /usr/... path. Otherwise, query the location using Sys.getenv("RSTUDIO_PANDOC") from an interactive session where the knitting works, and substitute the path in the above command.
Try
25 10 * * * cd /path && Rscript -e 'rmarkdown::render("test_doc.Rmd")'
which avoids
The full path and gives rmarkdown and knitr a better working directory
The need to 'quote quotes' by having apostrophes on the outside and standard double quotes on the inside.
Add the path to the beginning of your cron, and redirect the output for debugging purposes:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
*/5 * * * * cd /path/to/script/ && Rscript -e 'library(rmarkdown); rmarkdown::render("your_script.Rmd")' >/path/to/script/cron.log 2>/path/to/script/cronerr.log

R script as cronjob not executing

I've created a short R script that continuously downloads data from twitter using the streamR package. This script is supposed to run on a standard Amazon EC2 server running Ubuntu 14.04. When testing it in the standard command line, it runs fine. However, it is not run as specified in the cronjob. I used the following command:
sudo crontab -e
and then added the following line to the file
0 * * * * Rscript /home/mydirectory/docs/phd-research/data-collection/cron-script.R
in the hope that it would execute the R script every hour.
Is there anything I might have got wrong with the permissions? I've already checked that cron is running and I chmodded the directory the r script is supposed to write to to 775.
Thanks in advance!
OK - it turns out I used the wrong command to call up the crontab. The directory it was supposed to be working in was owned by user rather than root, so calling a root crontab didn't work.
The proper command for editing the crontab is the following:
crontab -u user -e
The Rscript didn't require the full path, it worked with both.
The $PATH$ is not known (i.e. the directory of Rscript is not part of the standard $PATH$) during the execution of a cronjob. Writing the full path to Rscript will work:
0 * * * * /.../Rscript /home/mydirectory/docs/phd-research/data-collection/cron-script.R

Schedule R script using cron

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
Consider these tips
Use Rscript (or littler) rather than R CMD BATCH
Make sure the cron job is running as you
Make sure the script runs by itself
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....

Resources