4

I'm running an R program on a linux cluster because it is very demanding on my processor. My program is designed to output multiple (around 15) plots as PDF's into the folder from which the program gathers its input.

I want my program to run in the background, and to continue running when I log out of the cluster.

First, I tried this:

cd /Users/The/Folder/With/My/RScript #changed working directory
nohup ./BatchProgram.R &

However, this didn't work because it appended the output to a file called nohup.out, and did not output any of the PDF's I need.

Next I tried this:

cd /Users/The/Folder/With/My/RScript #changed working directory
R #to run R
source(‘BatchProgram.R’) #to run my program

This gave me the desired output, but didn't run the program in the background (and would stop when I logged out of the cluster).

Could someone enlighten me as to how I might obtain the output of my second block of code, while running the program in the background AND causing it to continue running even after I log off of the linux cluster (like the first block of code)?

Many thanks!

MikeZ
  • 345
  • 1
  • 4
  • 15

2 Answers2

9

nohup runs a command in the background, makes it ignore signals telling it to stop (e.g., when you log off), and redirects the output to a file. But it has to be an executable command: you probably have error messages in nohup.out telling you that BatchProgram.R could not be run.

The following should work:

nohup Rscript ./BatchProgram.R &
Vincent Zoonekynd
  • 31,893
  • 5
  • 69
  • 78
  • This worked beautifully, thank you, Vincent! Just out of curiosity - what does the `Rscript` command actually do? – MikeZ Jul 24 '12 at 13:37
  • 2
    It just runs an R script, but without the interactive interpreter. It is used either on the command line, `Rscript my_script.R`, or on the first line of a script, `#!/usr/bin/Rscript`, to be able to run it directly, as `./my_script.R` (in this case, you also need to make the script executable, with `chmod +x`). It is almost equivalent to `R CMD BATCH`, but does not require additional options (`--slave`, in the other answer). – Vincent Zoonekynd Jul 24 '12 at 14:01
5

The solution is tested, u can try also making 'nohup' version of it, but what will be presented suits me well. Make bash script as follows (for example run_R.sh):

#!/bin/sh
R CMD BATCH --slave ./_your_R_script_name.R &
exit 0

then, make it runnable

chmod +x run_R.sh

and run file like,

./run_R.sh

sometimes you can get only blinking cursor (depends on what linux distro you're working) but simple press "enter" to continue

java_xof
  • 439
  • 4
  • 16
  • +1. I prefer using `R CMD BATCH` since it outputs everything to a "log" file, aka an `.Rout` file. – Banjer Aug 25 '14 at 13:40