Because screen doesn't implement -c like a normal shell, I can't use it
as my login shell without breaking scp(1) and suchlike.  Thus for some
time I've had this in my .bash_profile:

    ## The naive "chsh -s /usr/bin/screen" breaks scp (and other things).
    ## Have sh start screen automatically "when appropriate".  That is,
    ## 1) not in a screen window (STY);
    ## 2) stdin is a tty;
    ## 3) screen is installed; and
    ## 4) terminal is capable of displaying screen.
    ## Replace "$STY" with "$STY$SSH_CLIENT$SSH_ORIGINAL_COMMAND" to avoid
    ## nesting screens when sshing (untested).
    test -z "$STY" -a -t 0 &&
    { which screen && tput clear && tput cup && ! tput hc && ! tput os
    } &>/dev/null &&              # ignore boring, expected error messages
    {
        ## If new session, start agents.
        screen -ls | grep -qi 'No Sockets found' &&
        eval "$(twb-agents)"
        ## Start screen.
        exec screen -DRR
    }

The other advantage of this hack is that my login shell, stored in NIS,
remains valid on hosts where neither screen nor tmux are installed.  It
is for this reason that I wish to extend the code above to auto-start
tmux, rather than simply making tmux my login shell.

The implementation below seems to work, but I suspect it's neither
correct nor ideal.  I'd appreciate any critique you care to give.  Bash
and a GNU userland can be assumed, but I can't assume RECENT ones (some
hosts are still Fedora Core 1).

    test -z "$STY$TMUX" -a -t 0 &&
    {
        which tmux &>/dev/null &&
        {
            tmux list-sessions &>/dev/null ||
            {
                eval "$(twb-agents)"
                tmux new-session
            }
            exec tmux attach-session -d
        }

        { which screen && tput clear && tput cup && ! tput hc && ! tput os
        } &>/dev/null &&              # ignore boring, expected error messages
        {
            ## If new session, start agents.
            screen -ls | grep -qi 'No Sockets found' &&
            eval "$(twb-agents)"
            ## Start screen.
            exec screen -DRR
        }
    }


PS: twb-agents is basically a NIH rewrite of Gentoo's keychain(1), see
http://twb.ath.cx/Preferences/.bin/twb-agents .


------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to