全局 /etc/profile setenv 或导出覆盖 .profile 中的用户设置

Dav*_*vid 6 tcsh centos path shell-script

我需要在 CentOS 5.3 集群中调用程序的修改版本/home/user_name/bin而不是(旧)版本/opt/program_name/current。该程序使用 python 创建一个 tcsh 脚本,然后执行该脚本。

我可以覆盖 PATH 以在 中调用程序的修改版本~/bin,以便which program_name标识到~/bin的路径。我已经在这样做了.bashrc,.profile,.tcshrc,和.cshrc。但是,每次执行 tcsh 时,都会/etc/profile加载来自的环境变量,并且路径将恢复为/opt/program_name/current.

这些程序特定的变量在/etc/profile.d/program_name.sh被调用时设置/etc/profile:

# /etc/profile 
for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        . $i
    fi
done
unset i
Run Code Online (Sandbox Code Playgroud)

因此,当program_name.sh被调用时,对应的脚本是:

# /etc/profile.d/program_name.sh
setenv PATH ${PATH}:/opt/program_name/current
Run Code Online (Sandbox Code Playgroud)

如何防止/etc/profile被我的用户帐户访问?

我没有 su 访问权限,其他用户仍在使用旧版本的软件,/opt/program_name/current因此我无法更新软件或修改其中的设置/etc/profile.d/program_name.sh

Gil*_*il' 1

tcsh 不会自发读取/etc/profile,脚本中一定有某些内容导致了这种情况。一种可能的可能性(但不是唯一的可能性)是脚本以 开头#!/bin/tcsh -l,因此 tcsh 充当登录 shell。这仍然不会导致它读取/etc/profile,但它会导致它读取/etc/csh.login,这可能会读取其他文件(我不知道 CentOS 附带什么,csh.login也不知道您的系统管理员如何定制它)。

\n\n

读取后/etc/csh.login,tcsh 读取~/.login。根据它的配置方式,它可能会~/.login在之前、之后或代替~/.tcshrc. 所以一定要尝试PATH覆盖~/.tcshrc。

\n\n

如果这不起作用,修改 Python 脚本可能是您最好的选择。如果代码位于模块中,您可以通过放置一个包装器模块来解决它,PYTHONPATH该模块会加载原始代码并进行一些更改。

\n\n

如果失败,则没有简单的方法可以 \xe2\x80\x9credirect\xe2\x80\x9d 访问程序可执行文件或 tcsh。原则上,通过使用LD_PRELOAD包装一些文件访问函数是可能的(如果涉及的所有程序都是动态链接的并且没有设置[ug]id),但工作量很大。

\n\n

很可能有一种更简单的方法,可以通过挂钩到脚本的某个地方,例如,使用.cshrc. 如果没有看到那个脚本,很难说得更准确。

\n