I'm trying to make a script that opens up 4 tmux panes, and launch a python program inside of it. I want to do it simultaneously, but I don't know how.

I tried

tmux new-session "python 'DogeClickBot (2)'/main.py $1 ; read\" && tmux split-window \"python LTCClickBot/main.py $1 ; read"

The first Python program launches, but not the second. Please help!


The escaped double-quotes make tmux run the following in a new session:

python 'DogeClickBot (2)'/main.py substituted1 ; read" && tmux split-window "python LTCClickBot/main.py substituted1 ; read

and because of quoting read && tmux split-window python is interpreted as a single command name, as if you invoked

python … ; "read && tmux split-window python" …

This makes no sense.

Unescaped double-quotes make more sense:

tmux new-session -d "python 'DogeClickBot (2)'/main.py $1 ; read" && tmux split-window "python LTCClickBot/main.py $1 ; read"

This way in the current shell you run tmux new-session … && tmux split-window …, which is probably what you wanted.

Note I added -d because without it the first command would attach to the session and the next part (&& …) would not be executed until you detached or the session was destroyed. The right thing is to attach at the very end (tmux attach).

Be aware this straightforward approach may introduce a race condition: what if the session dies before tmux split-window runs? More about it in this another answer of mine. The most relevant fragment:

tmux new-window … [tmux new-session … in your case] is a command for the tmux server, tmux here is just a client. After the client exits successfully, you can be sure a new window [session] has been created; but you cannot really know what happens in it, on what stage, or if the window [session] has not been already destroyed.

Additionally ask yourself if you don't want to quote whatever $1 expands to. In this fragment:

tmux new-session "python 'DogeClickBot (2)'/main.py $1 ; read"

the parameter is properly double-quoted. Then the shell spawned by the tmux server will run this:

python 'DogeClickBot (2)'/main.py substituted1 ; read

where substituted1 is what $1 expanded to. Leaving it unquoted at this stage can have similar consequences as leaving $1 unquoted. Maybe this is what you want, maybe not. If not, then most likely you need:

tmux new-session "python 'DogeClickBot (2)'/main.py '$1' ; read"

and similar additional quoting in the second command.

  • Well, nothing happens. There's no error message. – KrystStalin Apr 20 '20 at 11:01
  • 1
    @KrystStalin Did you attach? Note I used new-session -d. – Kamil Maciorowski Apr 20 '20 at 11:02
  • I tried with tmux new-session -d "python 'DogeClickBot (2)'/main.py $1 ; read" && tmux split-window "python LTCClickBot/main.py $1 ; read" – KrystStalin Apr 20 '20 at 11:06
  • @KrystStalin So you didn't. Attach then: tmux a – Kamil Maciorowski Apr 20 '20 at 11:07
  • @KrystStalin You're using split-window without specifying the target (-t). This may be the culprit if the newly created session is not the only session. Please see man 1 tmux and learn how tmux chooses targets implicitly. Name the session and the window (new-session -s foo -n bar) and then target them explicitly split-window -t foo:bar. Is it better? – Kamil Maciorowski Apr 20 '20 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.