3
votes

I have a script I want to run where I would start up two programs running inside of separate tmux sessions. The script I have now is pretty much:

!/bin/bash
tmux new -s test1 'mono --debug program1.exe'
tmux new -s test2 'python program2.py'

The problem I am having is the 2 programs are running in debug mode so they are actively outputting information to the tmux session. I'm not able to gain control to type anything into the tmux session after starting the program. I can however manually detach the session using the Ctrl + b d method. I'm not sure how to go about doing that in a bash script though.

I found the tmux detach command but since I am not sure how to type into the session after the program has started and debug information is being output I have no way of entering that command.

I also found one post that was saying there is a -d flag you can use for tmux that will start a session detached and I was hoping I could do something like tmux new -d test1 'mono --debug program1.exe' but that didn't seem to work. It seemed to complain about syntax in the -d flag.

2

2 Answers

2
votes

You simply need to add the -d flag to each new command, in addition to its other options.

#!/bin/bash
tmux new -d -s test1 'mono --debug program1.exe'
tmux new -d -s test2 'python program2.py'

Note that you now have two sessions, either one of which you could attach to with tmux attach -s test1 or tmux attach -s test2. It might be simpler to run each command in a separate window of the same session:

tmux new -d -s test1 'mono --debug program1.exe'
tmux new-window 'python program2.py'
tmux attach -t test1

Replace new-window with split-window to run the commands in separate panes in the same window.

2
votes

To launch two separate sessions each running a program or command, without attaching, try writing this in your script.sh:

#!/bin/bash

tmux \
    new \
        -d \
        -s test1 \
        'mono --debug program1.exe' \
    \; \
    new \
        -s test2 \
        -d \
        'python program2.py'

Explanation

  • \ is for line continuation in bash, a good practice to break an otherwise long command to see it more clearly by having each option on its own line.
  • \; to allow us to add another tmux command to our original tmux call instead starting another tmux call form bash
  • -d to run the session detached. Its placement as you can see is flexible as long as it is after the new
  • new is an alias, short for new-session

So when you run the script, for example

$ ./script.sh

It quietly starts these tmux sessions. You can check they exist:

$ tmux ls
test1: 1 windows (created Sun Mar 13 15:19:31 2016) [79x18]
test2: 1 windows (created Sun Mar 13 15:19:31 2016) [79x18]

And attach to view them, for example test1:

$ tmux attach -t test1