bash 如何通过运行脚本让我的所有终端/外壳重新加载它们的环境?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7754661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get all my terminals/shells to reload their environment by running a script?
提问by Tom
I use my mac at home and at work. And I use a program called Marco-polo to detect whether I am at home or at work. Through this, I can get it to run a script that changes my proxy and run some scripts to configure my computer differently for the different environments like copying hosts.work and hosts.home over /etc/hosts and copy .profile.work and .profile.home over ~/.profile.
我在家里和工作中使用我的 mac。我使用一个叫做 Marco-polo 的程序来检测我是在家还是在工作。通过这个,我可以让它运行一个脚本来改变我的代理并运行一些脚本来为不同的环境配置我的计算机,比如通过 /etc/hosts 复制 hosts.work 和 hosts.home 并复制 .profile.work 和 . profile.home 在 ~/.profile 之上。
What I have not been able to do successfully so far, is to find a way to have all my running terminals reload my .profile file when I change location. Can anyone suggest a method for doing this?
到目前为止,我未能成功的是找到一种方法,当我更改位置时,让所有正在运行的终端重新加载我的 .profile 文件。任何人都可以建议这样做的方法吗?
Thanks, Tom
谢谢,汤姆
fm48 answer below in combination with this simple script (placed at /usr/bin/pkill) worked perfectly.
下面的 fm48 答案与这个简单的脚本(放在 /usr/bin/pkill 中)完美结合。
#!/bin/sh
sig=""
if [[ "" =~ - ]]; then
sig=;
shift
fi
for X in `ps acx | grep -i | awk {'print '}`; do
kill $sig $X;
done
回答by f4m8
You should use a signal like SIGUSR1. First enable the signal to reload the ~/.profile with trap ". ${HOME}/.profile" SIGUSR1.
您应该使用像 SIGUSR1 这样的信号。首先启用信号以重新加载 ~/.profile trap ". ${HOME}/.profile" SIGUSR1。
After that you should send all shells the SIGUSR1 signal. For example pkill -SIGUSR1 bashif bash is the used shell.
之后,您应该向所有 shell 发送 SIGUSR1 信号。例如,pkill -SIGUSR1 bash如果 bash 是使用的 shell。
回答by frankc
If you adopt Screen as a terminal manager, you can send commands to all your shells with:screen -X at \# stuff "whatever command\n"
如果您采用 Screen 作为终端管理器,您可以使用以下命令向所有 shell 发送命令:screen -X at \# stuff "whatever command\n"

