在不重启Ubuntu的情况下全局设置环境变量 [英] Setting environment variable globally without restarting Ubuntu
问题描述
我知道可以通过将条目添加到其中来设置系统范围的环境变量
I know that system wide environment variables can be set by adding entries into
/etc/environment
或
/etc/profile
但这需要系统重新启动或X重新启动.
But that requires system restart or X restart.
是否可以在Ubuntu/Linux中设置环境变量,以便在不重启操作系统或注销用户的情况下立即在整个系统范围内可用?
Is it possible to set an environment variable in Ubuntu / Linux so that immediately available system wide without restarting OS or logging out the user?
推荐答案
此perl程序使用gdb将当前正在运行的所有bash shell中的USER变量更改为程序arg给出的值.要设置新变量,可以使用内部bash调用"set_if_not"
This perl program uses gdb to change the USER variable in all currently running bash shells to whatever is given as the program arg. To set a new variable the internal bash call "set_if_not" could be used
#!/usr/bin/perl
use strict;
use warnings;
my @pids = qx(ps -C bash -o pid=);
my $foo = $ARGV[0];
print "changing user to $foo";
print @pids;
open( my $gdb, "|gdb" ) || die "$! gdb";
select($gdb);
$|++;
for my $pid ( @pids ) {
print "attach $pid\n";
sleep 1;
print 'call bind_variable("USER","' . $foo . '",0)' . "\n";
sleep 1;
print "detach\n";
}
此 only 与bash一起使用(我在Ubuntu 10.04 LTS上的4.1版本中对其进行了测试),并且不会更改任意已经运行的程序的环境.显然,它必须以root用户身份运行.
This only works with bash ( I only tested it with version 4.1 on Ubuntu 10.04 LTS) and does not alter the environment for arbitrary already running programs. Obviously it must be run as root.
这篇关于在不重启Ubuntu的情况下全局设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!