1

I often have a need to create Linux scripts which contain module load and module unload commands. Is there a way to test if a module is already loaded before executing a module unload command?

The reason why I need to do this is that if I have a module unload command and the module in question is not already loaded, then it will result in error messages. I would like to avoid these error messages by testing for the module being already loaded, and unloading it only if this is the case.

FenderMan
  • 11
  • 1
  • 1
  • 4
  • According to your comment you don't seem to mean Linux kernel modules but a less commonly used software, I guess this: http://modules.sourceforge.net/, https://www.mpcdf.mpg.de/services/computing/software/modules Please [edit] your question and add this information. Please show the exact commands you would use manually and the output of `module list`. – Bodo Jun 18 '19 at 09:32
  • The load and unload commands are 'module load ' and 'module unload – FenderMan Jun 19 '19 at 12:54
  • Here is the output of the module list command: Currently Loaded Modulefiles: 1) unix/1.0/b 6) questa/10.2c/a 2) ede/1.0/b 7) slickedit/2015/a 3) lsf/8.0/a 8) synplifypro/j2014.09sp1/a 4) adobereader/9.4-1/a 9) ise-ds/14.7/a 5) git/2.19.1/a – FenderMan Jun 19 '19 at 12:54
  • could you precise the version of module you use (type `module -V`) ? `module` command does not shoot and error when a not loaded module is unloaded – Xavier Delaruelle Jun 19 '19 at 17:50

3 Answers3

1

Recent versions of the module command (4+) have a new sub-command called is-loaded which returns true or false whether a given module is loaded or not.

So to conditionally unload a module from a shell session, you could type:

module is-loaded $MODULENAME && module unload $MODULENAME
  • Thanks, this is the kind of solution I was looking for. Unfortunately, the version I have doesn't support it. – FenderMan Jun 24 '19 at 12:57
0

Assuming the question is not related to Linux modules but to this software: https://modules.readthedocs.io/en/stable/module.html

The grep pattern assumes that the module name is the first word of the module list output. This will be fixed when the missing information is added to the question.

# example for unloading
MODULENAME=foobar
module list | grep -qw "^$MODULENAME" && module unload "$MODULENAME"
# example for loading
MODULENAME=foobar
module list | grep -qw "^$MODULENAME" || module load "$MODULENAME"
Bodo
  • 9,287
  • 1
  • 13
  • 29
  • Actually, "module list" will display a list of the modules that are loaded. What I really am looking for is something like the following:if [ ]; then module unload $MODULENAME fi – FenderMan Jun 17 '19 at 19:49
0

OK I think the answer of Bodo is close but this is tricky because at least my module command prints to the error output but I think OK solution for loading modules would be:

function LoadModule()
{
    if module list 2>&1| grep -qw $1; then
        echo "Module $1 already loaded."
    else
        echo "Loading module $1."
        module load $1
    fi
}

function UnloadModule()
{
    if module list 2>&1| grep -qw $1; then
        echo "Unloading module $1."
        module unload $1
    else
        echo "Module $1 is not loaded."
    fi
}
VojtaK
  • 483
  • 4
  • 13