Home > bashism, linux, software > Lmod and Sudo: ‘module: command not found’

Lmod and Sudo: ‘module: command not found’

September 11th, 2015 Leave a comment Go to comments

Recently, I switched from Environment Modules to Lmod because of … reasons. I really like it so far but one thing annoyed me a bit. When executing sudo -s which is one of my usual workflows, my shell reports:

module: command not found

The reason is simple: sudo dumps most of the users environment with some exceptions, due to env_reset in /etc/sudoers, when spawning the new non-login shell. Specifically the last fact causes the trouble since Lmod is activated by sourcing the proper file on (login-)shell startup. In my case (following the Lmod docs) there is a soft link in /etc/profile.d pointing to /usr/share/lmod/lmod/init/profile.

My first attempts to modify the env_keep list in /etc/sudoers didn’t work, because the module command itself is set as a shell function which AFAIK cannot be handled by that list of kept variables.

from the shell environment:

BASH_FUNC_module%%=() { eval $($LMOD_CMD bash “$@”);
[ $? = 0 ] && eval $(${LMOD_SETTARG_CMD:-:} -s sh)

$ type module
module is a function
module ()
eval $($LMOD_CMD bash “$@”);
[ $? = 0 ] && eval $(${LMOD_SETTARG_CMD:-:} -s sh)

So I saw no other solution but adding the following code snippet to my /etc/bash.bashrc

if [ -z "$LMOD_CMD" ]; then
. /etc/profile.d/modules.sh

This will test if there is a LMOD_CMD environment variable set. If not, it will source the proper file to activate Lmod.

Feel free to post if there are better solution! Cheerio!