Running R in the Background

Basically you want to run something like
$ R CMD BATCH infile outfile &
This may give you more output in outfile than you really want. If you want finer control over what is saved, experiment a bit with the following to figure out how to get what you want.

To stop all the usual R command line stuff from being written to the outfile, make

the first line in infile.

Another thing that is useful is at the end of your file you can have a line like

save(obj1, obj2, obj3, file = "sim1.Rdata")
This will save obj1, obj2, and obj3 (they might be some objects containing simulation results) in the file sim1.Rdata. If you do this and run the simulation with a command like
$ R CMD BATCH --no-save infile outfile &
then nothing will be saved in the .Rdata file. Actually, I usually use something like
$ R CMD BATCH --no-save --no-restore infile outfile &
This runs the commands in infile (I would call it infile.R or infile.Rbatch) with a "clean" R, i.e., it does not read the .Rdata file in the current directory. However, if you do it this way you have to remember to source the functions that you need.

Set limits to prevent runaway programs

There are many possible settings that can prevent an R job from eating up too much memory and ruining performance for the machine's owner:
user@host:~$ ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 20
file size               (blocks, -f) unlimited
pending signals                 (-i) 16382
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) unlimited
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
Here are some reasonable defaults, which are high enough to stay out of the way of normal programs:
$ ulimit -f 262144	# 2^(30-12) 4K blocks = 1 Gigabyte
$ ulimit -m 1048576	# 2^(30-10) kbytes = 1 Gigabyte
$ ulimit -t 259200	# 24*3600*3 seconds = 3 days
$ ulimit -v 1048576	# 2^(30-10) kbytes = 1 Gigabyte

(C) University of Florida, Gainesville, FL 32611; (352) 392-1941.
This page was last updated Tue Sep 25 00:34:40 EDT 2012