Search code examples
rslurmenvironment-modules

Logic to test whether Linux environment modules are needed


I help maintain an R package that auto-generates shell scripts for using Slurm to run Rscript on a remote server. There is a template for a submit.sh script that is filled in with arguments supplied by the user in an R function. However some systems are configured with environment modules so that the shell script needs to have the line module load R before Rscript is called.

Is there some logic I can execute from within R to detect whether module load R is necessary so that the line module load R is included in the shell script, conditional on whether or not it's necessary?

Link to the source code of our R package: rslurm
Template for submit.sh: submit.sh.txt


Solution

  • From the shell script, you may check if the module shell function is defined, then check if the R module exists in which case an attempt to load this module should be made:

    type module >/dev/null 2>&1
    if [ $? -eq 0 ] && module is-avail R; then
        module load R
    fi
    

    If the R module is already loaded, the module load command will do nothing. So it is safe to use it even if R module is already loaded.