Schedule a Rscript crontab everyminute - r

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.

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"

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

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.

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")'

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.

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

How do I add a cronjob to a makefile?

How do I add a cronjob to a makefile so when I compile a C program, I add a cron job to crontab so that program gets executed every minute:
Cron job I am trying to add:
* * * * * /Users/jenna/desktop/ && ./myProgram command_line_arg1
how do I add this to a make file ?
I believe it is a bad idea to add a crontab job from a Makefile. Adding a cron job is a sysadmin issue. Compiling your program is for a developer's hat.
If you insist on doing that, you might use crontab -l > mycrontab to list the current crontab jobs, then appending with
echo '* * * * * cd /Users/jenna/desktop/ && ./myProgram command_line_arg1' \
>> mycrontab
you probably want to make that more robust (perhaps use grep or awk) to avoid the appending if the crontab already contains that. BTW, you probably want to use $HOME/Desktop (and even that might not work on a French configured user, it would be $HOME/Bureau).
Then, set the new crontab with crontab mycrontab
At last, running something every minute seems suspicious to me. What is your program doing?
I would suggest to at the very least prompt the user before updating his/her crontab file....
Perhaps you want the at command....

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

Issues with cron

I'm really stuck. I've used cron on many machines, but I simply can not get it to work on an Ubuntu server. I've tried everything I can think over the weekend and I'm baffled. I'm hoping someone can help me out.
I can verify that cron is running via pgrep cron. However, pgrep crond returns nothing. I am trying to run a simple shell script (test.sh) with cron:
#!/bin/sh
/usr/bin/touch /home/jarvis/test.txt
I have run chmod +x on the shell script and my crontab looks like this:
01 * * * * /home/jarvis/test.sh
I also have a new line ending after the line.
Try redirecting all output to a file and see if there are any error messages that can help you diagnose the problme, i.e.
01 * * * * /home/jarvis/test.sh > /tmp/jarvis_test.log 2>&1
Also, if you created any of those files from a windows environment, don't forget dos2unix filename
edit
My point is that you won't know for what your script is doing in the crontab environment unless you see if it is outputing an error message. Error messages also go to local crontab user's email, so try mail (as the same user) and see if you have a bunch of messages from your crontab.
pgrep crond returning nothing sounds like the problem, but I'm not a sysadmin. Maybe you want to flag this and ask for moderator to move to https://serverfault.com/
I hope this helps.
I think it is related to the user permissions. Try putting in default crontab hourly,weekly files, if it works there then your cron is good. Also, check /var/log/messages for any cron entry. View /etc/crontab for detailed configuration.

Resources