简体   繁体   English

如何通过运行脚本让我的所有终端/ shell重新加载他们的环境?

[英]How do I get all my terminals/shells to reload their environment by running a script?

I use my mac at home and at work. 我在家里和工作中使用我的mac。 And I use a program called Marco-polo to detect whether I am at home or at work. 我使用一个名为Marco-polo的程序来检测我是在家还是在工作。 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. 通过这个,我可以让它运行一个脚本来更改我的代理并运行一些脚本来为不同的环境配置我的计算机,例如将hosts.work和hosts.home复制到/ etc / hosts并复制.profile.work和。 profile.home over~ / .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. 到目前为止我无法成功完成的工作是找到一种方法让我的所有运行终端在我改变位置时重新加载我的.profile文件。 Can anyone suggest a method for doing this? 有谁能建议这样做的方法?

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 [[ "$1" =~ - ]]; then
  sig=$1;
  shift
fi

for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
    kill $sig $X;
done

You should use a signal like SIGUSR1. 您应该使用SIGUSR1之类的信号。 First enable the signal to reload the ~/.profile with trap ". ${HOME}/.profile" SIGUSR1 . 首先启用信号以使用trap ". ${HOME}/.profile" SIGUSR1重新加载〜/ .profile。

After that you should send all shells the SIGUSR1 signal. 之后你应该发送SIGUSR1信号的所有shell。 For example pkill -SIGUSR1 bash if bash is the used shell. 例如,如果bash是使用过的shell,则为pkill -SIGUSR1 bash

如果您将Screen作为终端管理器,则可以使用以下命令向所有shell发送命令:
screen -X at \\# stuff "whatever command\\n"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示Unix机器上使用的shell? - How do I display the shells used on my Unix machine? 在多个 shell 上运行 bash 脚本 - Running bash script on multiple shells 如何获得Netbeans 7.3.1在Mac OS X(10.8.3)上查看我的环境变量? - How do I get Netbeans 7.3.1 to see my environment variables on Mac OS X (10.8.3)? 我如何告诉(在脚本中)在 mac os x 中打开了多少个终端? - How can I tell (in script) how many Terminals are open in mac os x? 如何仅在Iterm2中使用我的zsh,而不是所有其他终端? - How to only use oh my zsh in Iterm2, and not all other terminals also? 如何从脚本中设置bash环境变量? - How do I set bash environment variables from a script? 如何在运行Shell脚本后使Mac“ .command”文件自动退出? - How do I get a Mac “.command” file to automatically quit after running a shell script? 为什么在终端中运行此 shell 脚本时会出错? - why do I get errors when running this shell script in terminal? 当使用 Rails 运行 byebug session 并且服务器超时时,如何获取我的文本 / cursor? - How do I get my text / cursor back when running a byebug session with Rails and the server times out? 如何重新配置​​我的Mac以使bash脚本像以前一样使用echo? - How do I reconfigure my Mac to get bash script to use echo like it used to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM