2

I am doing the following thing (this is just an example, commads are more complex):

xterm -fa "Inconsolata" -e tmux new-session -s alpha &
disown %%
tmux new-window -t alpha bash
tmux new-window -t alpha zsh 

...which works perfectly when I type it in the terminal, giving me an xterm with a tmux session with three windows. BUT if I put the thing in a script, it will stop working with;

 no server running on /tmp/tmux-1153/default
 no server running on /tmp/tmux-1153/default

error, and just one window in the new tmux session alpha.

After a bit of experiments, I discovered that it will work again if I add a

sleep 5

(or similar) between the disown and the tmux new-window command. Clearly, the xterm has not finished to set-up before this command is run, and so tmux new-window is run before the session is created unless I put a delay there.

It works, but it's not elegant. Is there a way to tell tmux to wait until the session alpha is up?

5
  • 2
    Why not just start the window with the session: new -s alpha -n alpha bash? Commented Jul 1, 2016 at 7:18
  • @jasonwryan I tried, but it's not working for my case --- I really want three windows in the new sessions... Commented Jul 1, 2016 at 7:50
  • 1
    Three? You are only calling one in your example... Commented Jul 1, 2016 at 7:55
  • @jasonwryan you're right, I've simplified this too much. Corrected. However, in the first case, I had two windows (the first one launched together with the xterm, and the second one opened by the tmux command) Commented Jul 1, 2016 at 9:19
  • See my answer here: stackoverflow.com/questions/5609192/… Commented Jul 1, 2016 at 19:35

1 Answer 1

2

You can do this in two ways:

  1. Use the sleep method. I see nothing really wrong about using sleep apart from sometimes failing if you choose a too short time. To fool-proof it, use

    while ! tmux has-session; do sleep 1; done
    

    or

    while ! tmux has-session -t alpha; do sleep 1; done
    
  2. Use something like xtoolwait that starts an X client (your terminal emulator) in the background and returns when it has mapped its window.

If you go with the second option (which may be best), remember to remove the & at the end of the command starting your terminal, and it also becomes unnecesary to disown the job.

The command becomes:

xtoolwait xterm -fa "Inconsolata" -e tmux new-session -s alpha
tmux new-window -t alpha bash

If xtoolwait is not already installed on your Unix, it's likely available as a package (it's been around for ages). Use your package manager of choice to install it.

EDIT: From comments it seems as if tmux takes too long to spawn after the terminal has mapped its window even if xtoolwait is being used. So in this situation, I would go with looping over a sleep 1 call until the wanted tmux session exists.

4
  • Hmmm... seems not packaged anymore. Found here: github.com/dlitz/xtoolwait, testing... (does not compile. More testing needed. Umphhh...) Commented Jul 1, 2016 at 9:22
  • Ok. It almost work. xtoolwait will wait until the xterm is mapped, but not until the tmux is up. So I can use a smaller sleep, but I still have to use a sleep (in my new example, it fails the first tmux, then the second succeed... Commented Jul 1, 2016 at 9:32
  • 1
    @Rmano while ! tmux has-session; do sleep 1; done. Answer updated, and I'll add more soon. Commented Jul 1, 2016 at 9:34
  • 1
    Yep, that works. The while with tmux has-session -t alpha (I have other sessions around) is doing the trick. I was even starting to look into some inotify trick on the socket, but this is more straightforward. Thanks! Commented Jul 1, 2016 at 9:40

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.