R script as cronjob not executing - r

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

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.

Use crontab to automate R script

I am attempting to automate a R script using Rstudio Server on ec2 machine.
The R script is working without errors. I then navigated to the terminal on RStudio Sever and attempted to run the R script using the command - Rscript "Rfilename" and it works.
At this point I created a shell script and placed the command above for running the R script in there. This shell command is also running fine - sh "shellfilename"
But when I try to schedule this shell command using crontab, it does not produce any result. I am using the following cron entry :
* * * * * /usr/bin/sh ./shellfilename.sh
I am using cron for the first time and need help debug what is going wrong. My intuition is that there is there is difference in the environments used by the command when I run it on terminal and when I use the same in crontab. In case it is relevant information - am doing all of this on a user account created for myself on this machine so would differ from admin account.
Can someone help resolve this issue? Thanks!
The issue arose due to relative paths used in the script for importing files and objects. Changing this to absolute path resolved the described issue.

CRON not executing R script file

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.)

scheduling meteor.js on crontab

I have a meteor.js application I've created to run locally on OSX on my laptop. Every so often, meteor stops running for unexplained reasons (like after the computer returns from sleep).
I'd like to schedule a process on cron to check every minute if meteor is working, and if it's not, launch it. I placed the following in my crontab:
* * * * * ps aux | grep meteor | grep -v grep || cd ~/path_to_my_meteor_project/; nohup meteor &
This command works at launching meteor when I enter it manually in terminal. But when scheduled in cron, it does not seem to do anything.
This is likely to be caused by the fact cron expects a binary file (or a script to launch) whereas, when typed in a terminal, your command is interpreted by bash, which understands logic such as ||.
In order to fix this, you will have to create a bash script containing the line of code working in your terminal, then ask cron to run it every minute.

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