Module load/unload commands in Linux scripts

FenderMan picture FenderMan · Jun 17, 2019 · Viewed 10.6k times · Source

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.

Answer

Xavier Delaruelle picture Xavier Delaruelle · Jun 19, 2019

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