Updating the WPAD file should work just fine for GUI browsers like Firefox (assuming they're configured to use it).
Command-line tools like
curl are much harder:
- They can't use a PAC or WPAD file
- You can't even change the environment variables in a process's parent process, let alone globally.
- And there's no way to tell a shell to re-source a file when it changes.
Except that there is a way to do the last item. In
bash, at least. You can use the PROMPT_COMMAND variable to tell
bash to source a script every time it displays the shell prompt. For example:
Modify your script so that it outputs the *_proxy variable settings to a file, e.g.
/var/tmp/proxy-settings.sh. Make sure that file is world-readable, with
cat <<__EOF__ > /var/tmp/proxy-settings.sh
Then write a shell script to source that file if it has changed, save it as, e.g.,
/usr/local/bin/update-proxy-vars.sh. Again, this should be word readable (
chmod a+r). It doesn't need to be executable, because it's going to be sourced, not executed:
# each individual shell needs it's own timestamp, because each one will
# read the proxy settings file at different times.
mkdir -p /tmp/.proxy-timestamps
if [ -e "$tsfile" ] ; then
# get the timestamp of the proxy-settings.sh file
# not all versions of `stat` can do this, and those that do
# have different and incompatible options. The following works
# for GNU `stat`.
proxy_timestamp=$(stat -c %Y /var/tmp/proxy-settings.sh)
# has the timestamp changed? if so, source the file.
if [ "$timestamp" != "$proxy_timestamp" ] ; then
echo "$proxy_timestamp" > "$tsfile"
Finally, set PROMPT_COMMAND to source this script every time it displays a prompt.
You should also run
rm -f /tmp/.proxy-timestamps/timestamp.$$ in your ~/.bash_logout script, to tidy up when bash exits.just
This is, IMO, ugly and a nasty hack, and it slows down
bash (which is no speed demon to begin with) because it has to run the
update-proxy-vars.sh every time it displays a prompt. Every single time you run a command. Or just hit enter with no command. Every prompt, it's going to have to run this script, read in a timestamp file, run
stat, compare timestamps, and maybe source another file if the timestamp has changed.
It would probably be faster to just source
/var/tmp/proxy-settings.sh every time, if it exists, whether it has changed or not, and not bother with the timestamps.
PROMPT_COMMAND='[ -e /var/tmp/proxy-settings ] && . /var/tmp/proxy-settings.sh'
But it works, which is the main thing, right?
Or you could get into the habit of remembering to manually source a file like
/var/tmp/proxy-settings.sh in every shell that needs it when it's needed.
You can probably do the same thing using the
$PS1 variable in other shells (like
ksh, etc). But this version using PROMPT_COMMAND is ugly and hacky enough. Doing it with
PS1 would be even worse.