Linux - 定制启动命令wrapper,免受环境影响&净化环境
问题
在集群中,用户的环境往往加载了很多工具,PATH变量与LD_LIBRARY_PATH变量存的路径都非常多。在不考虑性能的情况下,LD_LIBRARY_PATH路径太多,容易导致库冲突,因为不同工具的lib目录下可能存放着同名的库文件,但提供的函数或符号不一致。
wrapper改造已有工具
[thesre@centos8 ~]$ ls -A /apps/opensource/screen/4.1.0/cad_wrapper/bin/
screen .wrapper
[thesre@centos8 ~]$ cat /apps/opensource/screen/4.1.0/cad_wrapper/bin/.wrapper
#!/bin/bash -f
# Author: thesre
# Date: 2019.01.01
# Get the name invoked.
calledAs=$0
cmdName=${calledAs##*/}
cmdBinDir=${calledAs%/*}
wrapperDir=${cmdBinDir%/*}
toolDir=${wrapperDir%/*}
# Initialize the environment.
. $wrapperDir/env_init.sh
# exec the real command with its all commandline arguments.
exec $cmdName "$@"
[thesre@centos8 ~]$ cat /apps/opensource/screen/4.1.0/cad_wrapper/env_init.sh
# Author: thesre
# Date: 2019.01.01
export PATH=$toolDir/bin:/bin:/usr/local/bin:/usr/bin #将PATH固定死,因为我知道该工具不会调用其它路径下的二进制可执行文件,因此我大胆地清空其余PATH路径。
export LD_LIBRARY_PATH=$toolDir/lib:$LD_LIBRARY_PATH #如果不确定是否调用了环境中已有的库路径,则可以以前置追加的方式进行修改。
[thesre@centos8 ~]$ which screen #将screen这个wrapper所在的路径加到shell PATH变量后,执行which可看其位置是在$toolDir/cad_wrapper/bin路径下。
/apps/opensource/screen/4.1.0/cad_wrapper/bin/screen
总结
完毕。