Search code examples
macosbashterminalprofilereload

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. 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.

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?

Thanks, Tom


fm48 answer below in combination with this simple script (placed at /usr/bin/pkill) worked perfectly.

#!/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

Solution

  • You should use a signal like SIGUSR1. First enable the signal to reload the ~/.profile with trap ". ${HOME}/.profile" SIGUSR1.

    After that you should send all shells the SIGUSR1 signal. For example pkill -SIGUSR1 bash if bash is the used shell.