Skip to main content
Tidy code a little and add more comments.
Source Link
Roger Jones
  • 1.5k
  • 8
  • 14

Not tested on the Pi but I have this in .bash_profile on another machine to start (or reconnect to) a tmux session with a couple of commands running in windows 0 and 5 and four terminals when I login:

if which tmux 2>&1 >/dev/null; then
    # 
 If not inside a# tmux$TMUX session,variable reconnectis toset 'mysession'when running in a session.
    #
    # If we're NOT in a session AND we fail to attach
    #  to the 'mysession' doessession notTHEN
 exist start  #    1. Start a new session with HTOP running
    #  HTOP running and2. Add 4 new windows with terminals
    #    3. Add a final new window watching SYSLOGthe syslog
    #    4. Choose the first terminal window
    #    5. Connect to the new 'mysession' session
    # 
    test -z "$TMUX" && \
      ( tmux -2 attach-session -t mysession || \
        ( \
        tmux new-session -d -s mysession -n 'HTOP' '/usr/bin/htop'; \
        tmux new-window -t mysession:1 -n 'Term1'; \
        tmux new-window -t mysession:2 -n 'Term2'; \
        tmux new-window -t mysession:3 -n 'Term3'; \
        tmux new-window -t mysession:4 -n 'Term4'; \
        tmux new-window -t mysession:5 -n 'LOGS' '/usr/bin/watch tail -n 40 /var/log/syslog'; \
        tmux select-window -t mysession:1; \
        tmux -2 attach-session -t mysession \
        ) \
    )
fi

Not quite what you want (because this only starts the commands when I login the first time) but I think the key is you need to append your python command to the /usr/bin/tmux new -s dropx line in your script rather than use send-keys after.

Not tested on the Pi but I have this in .bash_profile on another machine to start (or reconnect to) a tmux session with a couple of commands running in windows 0 and 5 and four terminals when I login:

if which tmux 2>&1 >/dev/null; then
    # If not inside a tmux session, reconnect to 'mysession'.
    #
    # If 'mysession' does not exist start a new session with
    #  HTOP running and watching SYSLOG.
    # 
    # 
    test -z "$TMUX" && \
      ( tmux -2 attach-session -t mysession || \
        ( \
        tmux new-session -d -s mysession -n 'HTOP' '/usr/bin/htop'; \
        tmux new-window -t mysession:1 -n 'Term1'; \
        tmux new-window -t mysession:2 -n 'Term2'; \
        tmux new-window -t mysession:3 -n 'Term3'; \
        tmux new-window -t mysession:4 -n 'Term4'; \
        tmux new-window -t mysession:5 -n 'LOGS' '/usr/bin/watch tail -n 40 /var/log/syslog'; \
        tmux select-window -t mysession:1; \
        tmux -2 attach-session -t mysession ) )
fi

Not quite what you want (because this only starts the commands when I login the first time) but I think the key is you need to append your python command to the /usr/bin/tmux new -s dropx line in your script rather than use send-keys after.

Not tested on the Pi but I have this in .bash_profile on another machine to start (or reconnect to) a tmux session with a couple of commands running in windows 0 and 5 and four terminals when I login:

if which tmux 2>&1 >/dev/null; then
    # 
    # $TMUX variable is set when running in a session.
    #
    # If we're NOT in a session AND we fail to attach
    #  to the 'mysession' session THEN
    #    1. Start a new session with HTOP running
    #    2. Add 4 new windows with terminals
    #    3. Add a final new window watching the syslog
    #    4. Choose the first terminal window
    #    5. Connect to the new 'mysession' session
    # 
    test -z "$TMUX" && \
      ( tmux -2 attach-session -t mysession || \
        ( \
        tmux new-session -d -s mysession -n 'HTOP' '/usr/bin/htop'; \
        tmux new-window -t mysession:1 -n 'Term1'; \
        tmux new-window -t mysession:2 -n 'Term2'; \
        tmux new-window -t mysession:3 -n 'Term3'; \
        tmux new-window -t mysession:4 -n 'Term4'; \
        tmux new-window -t mysession:5 -n 'LOGS' '/usr/bin/watch tail -n 40 /var/log/syslog'; \
        tmux select-window -t mysession:1; \
        tmux -2 attach-session -t mysession \
        ) \
    )
fi

Not quite what you want (because this only starts the commands when I login the first time) but I think the key is you need to append your python command to the /usr/bin/tmux new -s dropx line in your script rather than use send-keys after.

Source Link
Roger Jones
  • 1.5k
  • 8
  • 14

Not tested on the Pi but I have this in .bash_profile on another machine to start (or reconnect to) a tmux session with a couple of commands running in windows 0 and 5 and four terminals when I login:

if which tmux 2>&1 >/dev/null; then
    # If not inside a tmux session, reconnect to 'mysession'.
    #
    # If 'mysession' does not exist start a new session with
    #  HTOP running and watching SYSLOG.
    # 
    # 
    test -z "$TMUX" && \
      ( tmux -2 attach-session -t mysession || \
        ( \
        tmux new-session -d -s mysession -n 'HTOP' '/usr/bin/htop'; \
        tmux new-window -t mysession:1 -n 'Term1'; \
        tmux new-window -t mysession:2 -n 'Term2'; \
        tmux new-window -t mysession:3 -n 'Term3'; \
        tmux new-window -t mysession:4 -n 'Term4'; \
        tmux new-window -t mysession:5 -n 'LOGS' '/usr/bin/watch tail -n 40 /var/log/syslog'; \
        tmux select-window -t mysession:1; \
        tmux -2 attach-session -t mysession ) )
fi

Not quite what you want (because this only starts the commands when I login the first time) but I think the key is you need to append your python command to the /usr/bin/tmux new -s dropx line in your script rather than use send-keys after.