2

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.

4
  • According to your comment you don't seem to mean Linux kernel modules but a less commonly used software, I guess this: modules.sourceforge.net, 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. Commented Jun 18, 2019 at 9:32
  • The load and unload commands are 'module load <modulename>' and 'module unload <modulename> Commented Jun 19, 2019 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 Commented Jun 19, 2019 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 Commented Jun 19, 2019 at 17:50

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this is the kind of solution I was looking for. Unfortunately, the version I have doesn't support it.
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"

1 Comment

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
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
}

Comments

0

Starting with version 4.2, module unload no longer gives an error message if the name is not found.

https://modules.readthedocs.io/en/stable/changes.html:

On versions 4.0 and 4.1, unloading an nonexistent modulefile generates an Unable to locate modulefile error. Starting with version 4.2, unloading a module only looks at loaded module list and does not trigger a modulefile search.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.