Hi All,

After reattaching to a screen session, some of the environment variables
need to be updated. I have seen some solution where the variables need to be
explicitly updated (sourcing files). I haven't found any automated solution
to this.

My implementation below can do this automatically and may be useful to
others. This is for bash but can be adapted for other shells. Just source it
from your .bashrc.

Thanks
-Surinder

--X----------------------------------------------------------------------------------------

#!/bin/bash

# screen-attach <SessionName>
#
# $ screen -ls
# There are screens on:
#         64809.pvt104    (Detached)
#         8086.resman     (Attached)
# ....
#
# $ screen-attach 8086.resman
#
# The complete name is needed as it needs to extract process Id from the
name.
# One could potentially relax this.
#     - put the variable in a file.
#     - Run a script in a new window of the to be attached screen that:
#           - sources the file
#           - uses STY env variable to get the process Id.
#
function screen-attach # {{{
{
    local STY=$1
    local VARS="SSH_AUTH_SOCK DISPLAY"
    VARSLOC=${HOME}/tmp/screen.$STY.vars

    echo > $VARSLOC

    for v in $VARS ; do
        val=`eval echo \\${$v}`
        echo "export $v=$val" >> $VARSLOC

        # setup environment for new shells
        screen -S $STY -X setenv $v "$val"
    done

    # force environment update in existing screens
    CHILDS=`ps -o  pid,ppid| grep ${STY/.*/} | awk '{print $1}'`

    for child in $CHILDS
    do
        # a copy of environment file is created for each screen.
        cp $VARSLOC  $VARSLOC.$child

        kill -USR1 $child # tell the child to reload the values
    done

    rm $VARSLOC

    screen -x $STY
}
#}}}

function sourcevars #{{{
{
    VARSLOC=${HOME}/tmp/screen.$STY.vars.$$
    if [ -f $VARSLOC ] ; then
        source ${VARSLOC}
        rm ${VARSLOC}
    fi
} #}}}

function trap_usr1 #{{{
{
    sourcevars
    echo "Updated Variables"
}
#}}}

if [ "x$STY" != "x" ]; then
    # handle SIGUSR1 if we are under screen session
    trap "trap_usr1" SIGUSR1
    sourcevars
fi

# END of File
_______________________________________________
screen-users mailing list
screen-users@gnu.org
https://lists.gnu.org/mailman/listinfo/screen-users

Reply via email to