GNU Screen运行bash init脚本

时间:2009-06-12 08:23:30

标签: bash gnu-screen

我确信在屏幕手册中有一个答案,但我找不到它! 我希望GNU屏幕生成的bash shell除了已经运行的.bashrc之外还要在文件中提供源代码。

我无法在.bashrc中调用该文件,因为在我们的网站上.bashrc文件会在登录时自动重新生成。

有什么想法吗?

编辑:

我创建了这个小脚本(screen_bash.sh):

bash --rcfile ~/.screen_bashrc

然后添加

shell $HOME/screen_bash.sh

到我的.screenrc

〜/ .screen_bashrc文件是

<my_setup_stuff>
export SHELL=bash

SHELL = bash是必要的,这样像vim这样的程序可以正确启动子shell。

3 个答案:

答案 0 :(得分:4)

您是否希望每次打开新的屏幕窗口时都会获取此文件?如果是这样,shell命令允许您覆盖创建新屏幕窗口时运行的内容(默认情况下它只是$ SHELL)。您可以将其设置为您选择的脚本,最后运行您的shell。

答案 1 :(得分:2)

screen bash --rcfile yourfile.rc

yourfile.rc应该来源.bashrc

编辑:这并不是你想要的,我只是意识到你可能希望它适用于由屏幕启动的所有 shell。

答案 2 :(得分:0)

我之前一直在这样做,但现在我意识到最好是作为系统初始化服务运行。您可以找到附加到this bug report的脚本。希望它将作为Gentoo中屏幕ebuild的一部分提供。我会在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
}