1

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?

  • 2
    Why not just start the window with the session: new -s alpha -n alpha bash? – jasonwryan Jul 1 '16 at 7:18
  • @jasonwryan I tried, but it's not working for my case --- I really want three windows in the new sessions... – Rmano Jul 1 '16 at 7:50
  • 1
    Three? You are only calling one in your example... – jasonwryan Jul 1 '16 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) – Rmano Jul 1 '16 at 9:19
  • See my answer here: stackoverflow.com/questions/5609192/… – jasonwryan Jul 1 '16 at 19:35
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.

  • Hmmm... seems not packaged anymore. Found here: github.com/dlitz/xtoolwait, testing... (does not compile. More testing needed. Umphhh...) – Rmano Jul 1 '16 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... – Rmano Jul 1 '16 at 9:32
  • 1
    @Rmano while ! tmux has-session; do sleep 1; done. Answer updated, and I'll add more soon. – Kusalananda Jul 1 '16 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! – Rmano Jul 1 '16 at 9:40

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.