Running R in the background

RLearner picture RLearner · Aug 26, 2016 · Viewed 11.1k times · Source

Hi this is a question which i am not sure how to frame.

I am running R from a remote server. My access to the remote server is via ssh@username and so on. After i access i have a command prompt which i invoke R and i am comfortable working on R.

Question 1 I have a large network (100k nodes) and would like to do community detection and would like to run it in the background such that if i close my terminal its keeps running until its finished saves the result in my R working directory.

I have tried using nohup R & but i am not sure the process completed successful.

Question 2 If i manage to implement Question 1, how could i continue to use R to perform other task? and

Question 3 How do i check the task in Q1 is still running.

my attempt on a script looks like this

#!/usr/bin/env Rscript
library(igraph)
library(data.table)
edgebetween <- function(x) {
  simplify(x, remove.multiple = F, remove.loops = T) 
   edge.betweenness.community(x, directed = T)
}
community.a.g3 <- edgebetween(a.g3)

i have saved the script in my working director as a.R

and used at the command prompt after changing to my working directory chmod +x a.R nohup ./a.R &

What would i need to correct. Thanks

Answer

Tod Casasent picture Tod Casasent · Aug 26, 2016

You want to execute R in "batch" mode. See ( https://stat.ethz.ch/R-manual/R-devel/library/utils/html/BATCH.html )

The command below is an example from those docs. The "&" says "run this separate from the user's login session" so when you log out, it continues to run.

R CMD BATCH [options] infile [outfile] &

You can also use nohup as discussed here ( http://streaming.stat.iastate.edu/wiki/index.php/Running_Jobs_in_the_Background )

nohup R CMD BATCH ./myprog.R &