简体   繁体   English

Linux 脚本中的模块加载/卸载命令

[英]Module load/unload commands in Linux scripts

I often have a need to create Linux scripts which contain module load and module unload commands.我经常需要创建包含module loadmodule unload命令的 Linux 脚本。 Is there a way to test if a module is already loaded before executing a module unload command?有没有办法在执行module unload命令之前测试模块是否已经加载?

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.我需要这样做的原因是,如果我有一个module unload命令并且有问题的模块尚未加载,那么它将导致错误消息。 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.我想通过测试已经加载的模块来避免这些错误消息,并且只有在这种情况下才卸载它。

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.最新版本的module命令 (4+)有一个名为is-loaded的新子命令,无论给定模块是否已加载,它都会返回 true 或 false。

So to conditionally unload a module from a shell session, you could type:因此,要有条件地从 shell 会话中卸载模块,您可以键入:

module is-loaded $MODULENAME && module unload $MODULENAME

Assuming the question is not related to Linux modules but to this software: https://modules.readthedocs.io/en/stable/module.html假设问题与 Linux 模块无关,而是与此软件有关: 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. grep模式假定模块名称是module list输出的第一个单词。 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"

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:好的,我认为Bodo 的答案很接近,但这很棘手,因为至少我的模块命令会打印到错误输出,但我认为加载模块的好的解决方案是:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM