How to schedule an R Script Cronjob in a linux server?
The following cron job will run Rscript scriptSecos.R
from the path /home/script2
, once a day, at 0:00 (midnight).
0 0 * * * cd /home/script2; Rscript scriptSecos.R >/dev/null 2>&1
If you want to save the output of the script to a file, change >/dev/null
with >/path/to/file
.
You can copy and paste this cronjob in your crontab file (You can open the file by using command crontab –e
)
Schedule R script using cron
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....
Schedule a Rscript crontab everyminute
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"
Rscript in Crontab not running in Ubuntu Server 18.04
Is it possible that it is running correctly and the results are not being captured? Cron won't return results to active terminal.
The following worked for me on an Ubuntu Server 16.04
First, confirmed that the test script return was expected when run from Rscript terminal:
root@mytester:~/myrscripts# Rscript test.R
returns
[1] "Hello, World!"
Second, setup cron job via:
crontab -e
entered this line
* * * * * Rscript ~/myrscripts/test.R >> ~/mycronlog.log
notice I am piping results to a log (appending)
Third, checked log after a few minutes:
root@mytester:~# cat mycronlog.log
[1] "Hello, World!"
[1] "Hello, World!"
[1] "Hello, World!"
[1] "Hello, World!"
[1] "Hello, World!"
In R, how can I schedule function execution in a cronjob-like way?
while ( as.numeric(format(Sys.time(),format = "%H")) %in% 8:17){
if(as.numeric(format(Sys.time(),format = "%H")) %in% 9:17){
# your code here
}
}
Cron Job with R and SQL Server
Yes to all!
Your choices for scripting are either Rscript or littler as discussed in this previous post.
Having struggled with connecting to MSSQL databases from Linux, my recommendation is to use RJDBC for database connections to MSSQL. I used RODBC to connect from Windows but I was never able to get it working properly in Linux. To get RJDBC working you will need to have Java installed properly on your Linux box and may need to change some environment variables (seems I always have SOMETHING mis-configured with rJava). You will also need to download and install the JDBC drivers for Linux which you can get directly from Microsoft.
Once you get RJDBC installed and the drivers installed, the code for pulling data from the database will look something like the following template:
require(RJDBC)
drv <- JDBC("com.microsoft.sqlserver.jdbc.SQLServerDriver",
"/etc/sqljdbc_2.0/sqljdbc4.jar")
conn <- dbConnect(drv, "jdbc:sqlserver://mySqlServer", "userId", "Password")
sqlText <- paste("
SELECT *
FROM SomeTable
;")
myData <- dbGetQuery(conn, sqlText)
You can write a table with something like
dbWriteTable(conn, "myData", SomeTable, overwrite=TRUE)
When I do updates to my DB I generally use dbWriteTable()
to create a temporary table on my database server then I issue a dbSendUpdate()
that appends the temp table to my main table then a second dbSendUpdate()
that drops the temporary table. You might find that pattern useful.
The only "gotcha" I ran into was that I could never get a Windows domain/username to work in the connection sequence. I had to set up an individual SQL Server account (like sa).
Related Topics
How to Avoid "No Such File or Directory" Error for 'Make Clean' Makefile Target
How to Find List of Odbc Drivers Installed on Linux
How to Avoid the Prompts While Using Azcopy on Linux in a Script
Split Delimited File into Smaller Files by Column
Coqide 8.5: No Syntax Highlighting on Linux
Setting Up Jenkins Slave on MAC Os
Block Device Information Without Mounting in Linux
Differenceamong Three Priorities Used in Linux Kernel
Comparing Numbers in Bash Scripting
How to Create a Zip File Without Entire Directory Structure
Anaconda Libstdc++.So.6: Version 'Glibcxx_3.4.20' Not Found
Extract Text Between Two Strings Repeatedly Using Sed or Awk
Best Text Search Engine for Integrating with Custom Web App
Errors While Installing Opencv and Ffmpeg Through Rightscripts
Making 'Long' 4 Bytes in Gcc on a 64-Bit Linux MAChine
Compile Linux Kernel (2.6) Module Including Non Kernel Headers