GNU Screen running a bash init script

浪尽此生 提交于 2019-12-10 13:48:38

问题


I'm sure there is an answer to this in the manual to screen, but I can't find it! I want the bash shell spawned by GNU screen to source in a file in addition to the .bashrc that it already runs.

I can't put a call to the file in .bashrc because on our site .bashrc files get regenerated automatically on login.

Any ideas?

EDIT:

I created this little script (screen_bash.sh):

bash --rcfile ~/.screen_bashrc

Then added

shell $HOME/screen_bash.sh

To my .screenrc

The ~/.screen_bashrc file was

<my_setup_stuff>
export SHELL=bash

The SHELL=bash is necessary so that programs like vim can launch sub-shells correctly.


回答1:


Do you want this file to be sourced every single time a new screen window is opened? If so, the shell command allows you to overwrite what is run when you create a new screen window (by default it's just $SHELL). You can set this to be a script of your choice that at the end runs your shell.




回答2:


screen bash --rcfile yourfile.rc

yourfile.rc should source .bashrc.

EDIT: This doesn't really do what you want, I just realised you probably want it to apply to all shells started by screen.




回答3:


I had been doing it before, but now I realised that it's better to run as a system init service. You can find my script attached to this bug report. Hopefully it will be available as part of screen ebuild in Gentoo. I'll keep it up to date on github.

start() {

for SCREENRC in /etc/screen.d/* ; do 

    SESSION="$(basename $SCREENRC)"

    ## I don't think there may be a security issue,
    ## provided that users will not be have write
    ## permission in /etc/screen.d/ and if anyone
    ## gained access to mod the session file, they
    ## are in already anyhow!
    BELONGS="$(stat $SCREENRC --printf=%U)"

    MYSHELL="$(getent passwd $BELONGS | cut -d: -f7)"


    COMMAND="/usr/bin/screen -- -U -D -m -c ${SCREENRC} -S ${SESSION} -t ${SESSION}"

    ## Why on earth would one write this ???
    #HOMEDIR="$(getent passwd $BELONGS | cut -d: -f6)"

    ebegin "Starting screen session ${SESSION} for ${BELONGS}"

    PIDFILE="/var/run/screen.${BELONGS}.${SESSION}.pid"

    start-stop-daemon \
        --env TERM="rxvt" \
        --env HOME="~${BELONGS}" \
        --env SCREEN_SESSION=${SESSION} \
        --user $BELONGS \
        --chdir "~${BELONGS}" \
        --make-pidfile \
        --background \
        --pidfile=${PIDFILE} \
        --exec ${COMMAND}
    eend $?
done

}




stop() {

## Perhaps we should determin this by pidfiles ...
## but this way is not bad either!
for SCREENRC in /etc/screen.d/* ; do 

    SESSION="$(basename $SCREENRC)"
    BELONGS="$(stat $SCREENRC --printf=%U)"

    PIDFILE="/var/run/screen.${BELONGS}.${SESSION}.pid"
    PROCESS="$(cat ${PIDFILE})"

    if [ -e /proc/${PROCESS}/status ]; then

    grep -i "Name:" /proc/${PROCESS}/status | grep -iq "screen" || continue

    ebegin "Stopping screen session ${SESSION} for ${BELONGS} (PID: ${PROCESS})"

    ## There other things we can try here ...
    ## perhaps add /etc/screen.d/$SESSION.stop

    ## It will CERTAINly kill the righ screen!
    CERTAIN="${PROCESS}.${SESSION}"
    env TERM="urxvt" \
        start-stop-daemon \
            --user ${BELONGS} \
            --exec /usr/bin/screen -- -S $CERTAIN -X quit
    eend $?

    fi

    rm -f $PIDFILE

done
}


来源:https://stackoverflow.com/questions/985585/gnu-screen-running-a-bash-init-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!