How to Schedule an R Script Cronjob in a Linux Server

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

  1. Use Rscript (or littler) rather than R CMD BATCH

  2. Make sure the cron job is running as you

  3. Make sure the script runs by itself

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



Leave a reply



Submit