2

I prefer to use tmux rather than the default linux virtual console (i.e. what agetty uses), so I put this in ~/.bashrc so that tmux runs automatically when I log in to a TTY:

if [[ "$(tty)" == /dev/tty* ]]; then
    tmux -u
fi

At first it seems like this works: tmux runs as soon as I login. However, when I exit tmux, tmux immediately runs a second time! Here is the complete behavior:

  • ctrl+alt+f2
  • Log in
  • Default prompt briefly visible before tmux runs
  • I'm in tmux
  • ctrl+d
  • The text [exited] briefly flashes, and then I'm back in tmux again
  • ctrl+d
  • Back in default terminal without tmux
  • ctrl+d
  • Now I'm completely logged out

I can't track this down - I've tried logging all sorts of things, setting environment variables, looking at $SHLVL. It actually looks like .bashrc gets run like 7 times each time I log in!

I don't normally use tmux from within X (I just use Konsole), but I tried setting bashrc to have tmux run for all non-tmux sessions, and this double behavior did not happen in Konsole.

I'm using:

What on could be causing this?

1
  • 1
    agetty does not produce virtual terminals. It uses terminal devices.
    – JdeBP
    Jan 8, 2017 at 10:16

1 Answer 1

1

Fix #1:

Couldn't track it down but managed to fix this with the following hack in my ~/.bashrc:

if [ -z "$user_bashrc_already_run" ]; then
    user_bashrc_already_run=1
else
    return
fi

Fix #2:

Managed to track this down properly. Since TTY is an interactive login shell it was running both /etc/profile and ~/.profile (terminal emulator in X loads neither, since it's not a login shell). In my case (and I have no idea why, maybe Bashish installation or something else) those files were identical, and both source all the scripts in /etc/profile.d, which in my case included one script which sources ~/.bashrc.

Removing all the duplicate code from ~/.profile fixed the problem.

Reading the "Invocation" section from man bash and questions like https://serverfault.com/questions/261802/what-are-the-functional-differences-between-profile-bash-profile-and-bashrc are useful.


In case anyone lands here with a similar issue, here is a better way to run tmux, because it causes the shell to be replaced with tmux instead of running tmux as a sub shell:

if command -v tmux>/dev/null; then # check if tmux command exists
    if [[ "$(tty)" =~ /dev/tty ]] && [[ ! "$TERM" =~ screen ]] && [ -z "$TMUX" ]; then
        # We're on a TTY and *not* in tmux
        exec tmux -u
    fi
fi

And I decided to put this in my ~/.profile because I want this running on login shells, not non-login shells. (Note that if you have ~/.bash_profile, bash will run that instead of ~/.profile.)

3
  • 2
    agetty has nothing to do with .profile. Neither do the terminal emulators. This is all the doings of your shell. You've been merrily setting things up to work with non-login interactive shells, not taking into account how things work with login shells.
    – JdeBP
    Jan 8, 2017 at 9:59
  • Read the bash man page to see how it loads from the various rc files on startup.
    – chicks
    Jan 8, 2017 at 12:51
  • Thanks for the pointers - feel like I've gone over these things a dozen times and it hasn't stuck (but hopefully this exercise has done the trick). I've updated my answer with the real root cause of the problem, and hopefully made it more... accurate overall.
    – tobek
    Jan 8, 2017 at 19:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .