I am running Ubuntu 16.04.3 LTS on the cloud. When I run a program from the command line with Rscript everything goes as expected. However, when I run the same program with Rscript through cron it appears that my .Rprofile file is not invoked. I have written a small program to demonstrate this problem:
test_cron = function() {
#The next 3 lines are base R.
sink('~/test_cron.out')
on.exit(sink())
cat('The date and time are:', as.character(Sys.time()), '\n')
#Now try to access a personal option, set by .Rprofile.
root = getOption('root')
cat('Option root:', root, '\n')
}
test_cron()
I run this from the command line using this command:
Rscript test_cron.r
The cron_test.out file contains the following:
The date and time are: 2017-11-14 06:15:46
Option root: /home/ubuntu/_algi/
The relevant line in crontab is as follows:
20 6 * * * /usr/bin/Rscript ~/test_cron.r
When this is run by cron, cron_test.out contains the following:
The date and time are: 2017-11-14 06:20:01
Option root:
Evidently the program, when run by cron, could not access my personal option 'root'. This is one among a number of experiments I have run that convince me that .Rprofile is not invoked under cron. Is there a fix for this?
Note: The R_PROFILE_USER environment variable is set to point to my .Rprofile file. Apparently, Rscript under cron ignores it.