1

trying to setup a staging server for an API which I'm building using Django - and so far I was cutting corners, starting the thing using python manage.py runserver. But now that the setup grew a bit more complex, I decided to build an ansible playbok. Everything worked fine until I got to launching gunicorn - because I want it to run inside a tmux session. Manual process doesn't seem to trivially translate to ansible. I've been manually creating tmux session:

tmux new-session -A -s api

and then running gunicorn inside this new "environment" (subshell?)

The thing is (as is probably obvious to ansible veterans), when I get to running the first step, my playbook just hangs, and never gets to the next step, which is where gunicorn is to be started. I suppose this is because I'm starting a new shell with tmux, and ansible is lost, not hearing back (because, my guess, it's still waiting for a response on the original shell? which will never come). Is there a right way to execute the "tmux" step, letting ansible use it as a context/environment for the next step, or should I just be content with ansible doing the setup, and do the tmux thing manually? I had a similar problem, when dealing with the fact that gunicorn is inside a virtualenv, but the workaround is to use a full path, which includes the virtualenv guts. Not sure if there's a similar workaround with tmux...

thanks y'all

alexakarpov
  • 1,848
  • 2
  • 21
  • 39

1 Answers1

1

tmux immediately attaches to the new session, and doesn't exit until you detach from the session or the last process in the session ends. Until tmux exits, the rest of your script hangs.

You can use the -D option to prevent attaching to the session, whether or not it needs to be created.

tmux new-session -AD -s api

The rest of your script can now proceed.


tmux new-session -AD -s api is a shortcut for

tmux has-session -t api || tmux new-session -d -s api
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I see; I was using tmux to make sure the process stays alive when I disconnect; can I run the process (gunicorn) without attaching to the subshell? – alexakarpov Aug 24 '21 at 18:58
  • 1
    Yes. When you create a new session, it runs some command in the (only) pane in its (only) window. By default, that's a shell. But you can specify a different command, for example `tmux new-session -AD -s api gunicorn`. You can also interact with a detached session before attaching to it. For example, `tmux new-session -AD -s api -d gunicorn` starts `gunicorn` in window 1, and `tmux new-window -t api vim` starts up a vim editor in a new window. – chepner Aug 24 '21 at 19:15
  • /sigh -AD doesn't work (ansible hanging as before); -Ad _does_ work, but leaves me in a strange place... the process (gunicorn) _is_ running, but if I drop into the box, tmux lstells me off - no sessions, not even a server is running... the process which _is_ running my gunicorn has tmux command with all the params I provided, but the session is nowhere to be found... – alexakarpov Aug 25 '21 at 14:16
  • interestingly enough, turns out that the "shortcut" above is not a shortcut; running the full commands you gave after it does actually work, and produces the same result as -Ad mentioned already – alexakarpov Aug 25 '21 at 14:22
  • 1
    Update your question to show exactly how you are using Ansible to start the `tmux` session. Your version of `tmux` may also be relevant. – chepner Aug 25 '21 at 14:22
  • ugh, sorry for spamming y'all; I ended up creating another question to deal with a different angle of this same problem. it has the missing parts: https://stackoverflow.com/questions/68925949/running-server-in-tmux-through-ansible – alexakarpov Aug 25 '21 at 15:52