Reputation: 556
I am trying to run an R script as a cronjob with a conda environment. Running the script from terminal works well with following script.sh
/opt/anaconda/anaconda3/envs/{env-name}/bin/Rscript '/ABSOLUTE_PATH/script.R' >> '/ABSOLUTE_PATH/script.log' 2>&1
However, when defining the job in crontab -e
as
30 14 * * * /ABSOLUTE_PATH/script.sh
Results in
Error: package or namespace load failed for 'tidyverse' in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/opt/anaconda/anaconda3/envs/admenv/lib/R/library/stringi/libs/stringi.so':
libicui18n.so.58: cannot open shared object file: No such file or directory
Execution halted
I have tried to run it as
/opt/anaconda/anaconda3/envs/{env-name}/bin/R '/ABSOLUTE_PATH/script.R' >> '/ABSOLUTE_PATH/script.log' 2>&1
source /opt/anaconda/anaconda3/bin/activate {env-name}; Rscript '/ABSOLUTE_PATH/script.R' >> '/ABSOLUTE_PATH/script.log' 2>&1
which all resulted in the same error as a cronjob.
Reinstalling packages in conda conda remove r-tidyverse; conda install -c r r-tidyverse
did not help.
Used R version is 3.6.2 and Anaconda 4.8.2. How could I solve this problem?
Upvotes: 1
Views: 369
Reputation: 556
Thanks vahvero for the excellent reply. The problem seemed to be in my conda environment, so just recreating the environment with your instructions solved the problem!
Upvotes: 0
Reputation: 663
You are most likely using the environment wrongly. Conda cannot be directly called in a bash script, as per it wants to use conda init bash
to be used in shell.
I managed to make this work with following steps:
# Create the enviroment to be used
conda create -n r-env r-base r-essentials <other packages, for example r-tidyverse r-dbi>
Next create the script.sh
:
conda activate r-env
Rscript <ABSOLUTE_PATH>/script.R
Then call the cronjob as an interactive (the -i
option) shell which allows conda environment to be activated.
# You can use obliviously any time you like
* * * * * bash -i <ABSOLUTE_PATH>/script.sh <any error streams>
Tested with R script
library(tidyverse)
library(DBI)
library(ggplot2)
print("Hello world!")
which was piped to external file.
Upvotes: 2