Skip to main content
  1. posts/

Automation using Tmuxp

·546 words·3 mins·
Automation Tmuxp Tmux Cron
Mshnwq
Author
Mshnwq
Table of Contents

If you’re looking for a powerful tool to automate your workflow, tmuxp might be just what you need. tmuxp is a session manager for tmux a terminal multiplexer, which means it allows you to have multiple terminal sessions within a single terminal window. This can be incredibly useful for managing multiple tasks or monitoring multiple systems simultaneously. Tmux also allows you to detach and reattach sessions, so you can keep tasks running even when you’re not actively connected to the terminal.

Install tmuxp
#

First, you need to make sure that you have tmux installed here is how.

Then you can install tmuxp. You can do this using:

pip:

$ pip install --user tmuxp

Homebrew:

$ brew install tmuxp

Debian / ubuntu:

$ sudo apt install tmuxp

Create a Configuration File
#

Next, you’ll need to create a configuration file for your tmux session. This file will define the layout of your windows and panes, as well as any commands that should be run when the session is started.

Here’s an example configuration file (session.yaml):

session_name: my_session
windows:
  - window_name: main
    panes:
      - echo "Pane 1"
      - echo "Pane 2"
  - window_name: logs
    panes:
      - tail -f /var/log/syslog

Start the Session
#

Now that you have your configuration file, you can start your tmux session using tmuxp:

$ tmuxp load session.yaml

This will load the session defined in your configuration file, creating any necessary windows and panes, and running any specified commands.

tmuxp checks for configs in user directories:

  • $TMUXP_CONFIGDIR, if set
  • $XDG_CONFIG_HOME, usually $HOME/.config/tmuxp/
  • $HOME/.tmuxp/ the default dir I believe

To load your tmuxp config from anywhere by using the filename, assuming ~/.config/tmuxp/mysession.yaml:

$ tmuxp load mysession

See author’s tmuxp configs and the projects' tmuxp.yaml.

Automation Example
#

Ok so now to the good part, an example of use. Personally I found great success in using for keeping organized my deployment of various docker stacks and daemons.

The project structure for my servers usually looks something like this.

Project Structure
#

.
└── project
  ├── Apps                 # These Apps could be whatever
  │   ├── App1             # A Daemon App
  │   │   └── run.sh
  │   └── App2             # A Docker App
  │       └── docker-compose.yml
  ├── Scripts
  │   ├── start_tmuxp.sh   # To be ran on boot
  │   └── setup_tmuxp.sh   # helper make sure config is set
  └── Tmuxp
      ├── app1.yaml        # app1 tmuxp config
      └── app2.yaml        # app2 tmuxp config

setup_tmuxp.sh
#

#!/bin/sh
export TMUXP_CONFIGDIR="$HOME/.tmux/tmuxp"
cp ../Tmuxp/* $HOME/.tmux/tmuxp # needs to match the config dir

start_tmuxp.sh
#

#!/bin/sh
tmuxp load app1
tmuxp load app2

app1.yaml
#

session_name: App1
windows:
  - window_name: window
    shell_command_before:
      - cd path/to/project/Apps/App1
    panes:
      - shell_command:
          - ./run.sh
  - window_name: etc
    shell_command_before:
      - tmux detach # needed for the script to work

app2.yaml
#

session_name: App2
windows:
  - window_name: window
    shell_command_before:
      - cd path/to/project/Apps/App2
    panes:
      - shell_command:
          - docker-compose up
  - window_name: etc
    shell_command_before:
      - tmux detach # needed for the script to work

Now you should be able to

$ ./start_tmuxp.sh

and confirm that everything is working

Crontab
#

Finally you need to add the start_tmuxp.sh to you crontab.

This will ensure that your tmuxp sessions are started automatically on boot.

$ crontab -e

then add the following

@reboot /path/to/project/Scripts/start_tmuxp.sh

Now reboot and enjoy your organized sessions being persistant and easy to manage.