Bash scripts with tmux to run a 4-window window

Can someone explain what happens to tmux , bash and exec ? I am trying to setup a tmux session with a 4 window window. Ideally, I want to run a command in 3 of the panels: for example. Ruby Thin server and several Ruby daemons. This is what I still have:

~/.bin/tmux-foo :

 #!/bin/sh tmux new-session -d -s foo 'exec pfoo "bundle exec thin start"' tmux rename-window 'Foo' tmux select-window -t foo:0 tmux split-window -h 'exec pfoo "bundle exec compass watch"' tmux split-window -v -t 0 'exec pfoo "rake ts:start"' tmux split-window -v -t 1 'exec pfoo' tmux -2 attach-session -t foo 

~/.bin/pfoo :

 #!/bin/bash cd ~/projects/foo rvm use ree # here I want to execute command1 2 3 or 4... exec $SHELL 

Everything works ... but when I ctlr-c in the first panel on which the thin server is running, it stops the thin server and returns to the shell. However, the team is not in history; that is, if I press the up key, I do not get the bundle exec thin start command ... I get another command from my bash story. I am wondering if there is a way to organize these scripts in order to get commands in the bash history.

Also ... I tried many combinations of exec , exec $SHELL -s ... and exec $SHELL -s ... -I , and I'm not quite sure what is happening ...

Can someone explain the general idea of ​​what happens with tmux and bash and exec here?

+43
bash exec tmux
Mar 27 2018-11-11T00:
source share
3 answers

As already mentioned, your commands are run by the shell before running $SHELL ; There is no general way for the $SHELL instance to know what its parent started before starting it.

To get the β€œinitial command” in the shell history, you need to directly issue commands to the $SHELL instance (after it has been started, of course). In other contexts, I might suggest using the small Expect program to create an instance of $SHELL , passing it with keystrokes, and then use interact to associate tty with the expected-generated $SHELL .

But in the context of tmux, we can just use send-keys :

 #!/bin/sh tmux new-session -d -s foo 'exec pfoo' tmux send-keys 'bundle exec thin start' 'Cm' tmux rename-window 'Foo' tmux select-window -t foo:0 tmux split-window -h 'exec pfoo' tmux send-keys 'bundle exec compass watch' 'Cm' tmux split-window -v -t 0 'exec pfoo' tmux send-keys 'rake ts:start' 'Cm' tmux split-window -v -t 1 'exec pfoo' tmux -2 attach-session -t foo 
+44
Oct 11 2018-11-11T00:
source share

tmuxinator allows you to specify this using a good yaml file. For your case, you could:

 # ~/.tmuxinator/foo.yml # you can make as many tabs as you wish... project_name: foo project_root: ~/projects/foo rvm: ree tabs: - main: layout: tiled panes: - bundle exec thin start - bundle exec compass watch - #empty, will just run plain bash - rake ts:start 

Of course, you may have additional windows, etc.

+11
Apr 18 '12 at 20:01
source share

You execute the command and then enter the interactive shell; a command launched from a script that is not in an interactive shell is not written to history. You really want to use material (this is a technical term :), once it was TIOCSTI for input "terminal ioctl (): stuff input") for a shell in a window.

With tmux , it seems you are using buffers for this. Something along the lines (untested)

 #! /bin/bash cd ~/projects/foo rvm use ree if [[ $# != 0 ]]; then tmux set-buffer "$(printf '%s\n' "$*")" \; paste-buffer -d fi exec ${SHELL:-/bin/sh} 
+2
Mar 27 '11 at 5:26
source share



All Articles