Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run an R script at boot

First of all, this may need to be moved to superuser. I couldn't decide which venue was better.

I am trying to write an R script that will run at boot/reboot and add that machine to a pool of doRedis workers. (doRedis is a foreach backend).

Here is my R script, "~/Rworker.R"

#Define Parameters
require(multicore)
Host <- 'ip_of_doRedis_Server'
cores <- multicore:::detectCores()
TO <- 24*3600

#Start Workers
require('doRedis')
startLocalWorkers(cores,'jobs',host=Host,port=6379, timeout = TO)

I can run this script from the command line, using the command sudo R CMD BATCH ~/Rworker.R ~/RLog.

Next, I wrote a shell script to run the R script, titled "/etc/init.d/StartWorkers.sh"

#!/bin/sh
sudo echo "Starting R workers"
sudo R CMD BATCH ~/Rworker.R ~/RLog

I made this shell script executable, using chmod +x StartWorkers.sh. When I run ./StartWorkers.sh everything works great and the R session starts up and the workers get added to the pool.

Now, I need this shell script to run when I boot/reboot the machine, so I type update-rc.d StartWorkers.sh defaults. This command appears to work, but I get the following warning: 'update-rc.d: warning: /etc/init.d/StartWorkers.sh missing LSB information'

However, a check with rcconf confirms that "StartWorkers.R" is on the startuplist.

However, when I reboot the machine, the script fails to run. What am I doing wrong? The shell script runs fine from the command line, but fails when I try to run it at startup.

/EDIT: ok, per Dirk's answer, I installed littler, and changed 'StartWorkers.sh' to the following:

#! /usr/bin/r

#Define Parameters
require(multicore)
Host <- 'zachec2.dyndns.org'
cores <- multicore:::detectCores()
TO <- 24*3600

#Start Workers
require('doRedis')
startLocalWorkers(cores,'jobs',host=Host,port=6379, timeout = TO)

But when I run it, I get the following output:

Loading required package: utils
Loading required package: multicore
Warning message:
In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE,  :
  there is no package called 'multicore'
Error in loadNamespace(name) : there is no package called 'multicore'
Calls: ::: ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
Execution halted

I know I have multicore installed on my system!

/EDIT2: I had to move all my R packages to cd /usr/lib/R/site-library and now the littler shell script works. I added the script to /etc/rc.local and it starts up perfectly!

like image 668
Zach Avatar asked Jun 17 '11 19:06

Zach


People also ask

Can you run Rscript from terminal?

If R has been installed properly, simply entering R on the command line of a terminal should start the program. In Windows, the program is typically specified as the action performed when clicking on an icon.

How do I run an Rscript from the command line?

Running R from the Command Line To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line.


1 Answers

This is a bit of an R question, and a bit on an Ubuntu sysadmining question. here are a few points:

  1. For simple start-up tasks, I recommned just using /etc/rc.local where you can append you jobs.

  2. I just don't like R CMD BATCH which is why Jeff Horner and I wrote littler which gives you /usr/bin/r and much easier R scripting. R itself also gives you Rscript; either one is preferable over R CMD BATCH.

  3. To test scripts, just run them as root. Once theyw ork, add them to /etc/rc.local.

Hope this helps. The r-sig-debian list is a good source of Ubuntu / Debian tips too.

like image 97
Dirk Eddelbuettel Avatar answered Oct 11 '22 18:10

Dirk Eddelbuettel