Gnu Screen: Execute command on attach
You can run screen -S foo -X setenv DISPLAY "$DISPLAY"; screen -S foo -rd
to change the environment of the screen
process before attaching. This won't affect existing windows.
You can perhaps configure your shell to check for an updated DISPLAY
(and any other relevant variable such as XAUTHORITY
) each time they show a prompt. (This means you may have to press Enter once if the shell was sitting at a prompt when you attached the session.) Bash evaluates $PROMPT_COMMAND
before displaying each prompt. Zsh runs the precmd
function before displaying each prompt. For example, if you've put the desired environment assignments in a script ~/var/run/screen-12345.foo.env-update.sh
(where 12345 is the pid of the screen process and foo
is the session name), you can use something like (for zsh, untested; I don't think you'll be able to get away without forking in bash):
precmd () {
local now=$SECONDS
set ~/var/run/"screen-$STY.env-update.sh"(Nms-$(($now-$screen_env_time)))
if [[ $# -ne 0 ]]; then
screen_env_time=$now
. ~/var/run/"screen-$STY.env-update.sh"
fi
}
Another thing you could do from $PROMPT_COMMAND
or precmd
, on some operating systems, is read the environment of the parent process (this assumes you have updated the environment of the screen process). For example, on Linux with zsh (do this only if running under screen):
precmd () {
local record
while read -r -d $'\0' record /proc/$PPID/environ; do
case ${record%%=*} in
DISPLAY|XAUHORITY) export $record;;
esac
done
}
Technically, you can change the environment of another process, using a debugger. But there's a good chance that it'll crash that process, because the program's internal data structures won't match with the data kept by the kernel.
Note that none of these solutions will do any good if you run ssh inside a screen window.