Reputation: 945
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.
Upvotes: 1
Views: 430
Reputation: 945
It transpires that cron does not load the user's environment. This has caused endless confusion and anguish, with no commonly accepted solution. See, for example,
https://serverfault.com/questions/673480/load-users-environment-variables-in-a-cronjob
https://stackoverflow.com/questions/15557777/cron-job-does-not-get-the-environment-variables-set-in-bashrc
https://stackoverflow.com/questions/2229825/where-can-i-set-environment-variables-that-crontab-will-use
https://unix.stackexchange.com/questions/27289/how-can-i-run-a-cron-command-with-existing-environmental-variables
A closely related problem is that .bashrc will only run in an interactive shell::
https://unix.stackexchange.com/questions/257571/why-does-bashrc-check-whether-the-current-shell-is-interactive
My solution is to write a shell script in which I set the necessary environment variables, then run my program:
#!/bin/bash
export CODE_HOME='/home/ubuntu/'
export OS='Ubuntu'
export R_PROFILE_USER=~/R/.Rprofile
/usr/bin/Rscript ~/test_cron.r
After making the script executable, it successfully runs under crontab.
Upvotes: 0
Reputation: 2986
By default R looks for and runs .Rprofile files in the three locations in a specific order which are:
.Rprofile in you current project/wd will override .Rprofile in HOME and R_HOME and .Rprofile in HOME will override R_HOME.
So to to create a project-specific start-up script, simply create a .Rprofile file in the project’s root directory.
It looks like in your case that R uses a different .Rprofile file when a script is started by cron than starting a script from the command line.
Upvotes: 2