bash script - how to execute command after starting new tmux session

1.4k Views Asked by At

For example: start new tmux session, launch webserver (sends log into console), and launch second tmux window for editor.

I had trying do this by different ways - but nothing works.

3

There are 3 best solutions below

0
On

Take a look at tmuxify (disclaimer: I wrote it).

It's to automate the launch of tmux sessions by defining a layout file with windows, panes and commands.

0
On

You can run shell commands from ~/.tmux.conf:

if-shell shell-command export I_AM_USING_TMUX=1

But it often does not seem to work as expected...

The environment variable TMUX is set when you run tmux. Then you can conditionally run shell commands from ~/.bash_profile:

if [[ ! -z "$TMUX" ]]; then
    # run shell commands
fi      

Or, you can export your own environment variable from ~/.tmux.conf file (as I already exported one from the beginning) and use it for if condition from ~/.bash_profile again:

if [[ ! -z "$GHCHOI_IS_USING_TMUX" ]]; then
    # run shell commands
fi      
0
On

For commands executions, take a look at this answer.

But you can also predefining layouts of tmux sessions, try tmuxinator/tmuxinator .