Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Tmux

From Nest Guides

If you're like me, you often have a lot going on in your terminal. Editing a config file in one tab, an app running in the next, monitoring your proceses in another, and so on. It can be a little annoying to have a bunch of terminal tabs, and a separate SSH connection to Nest in each one. This is where tmux (terminal multiplexer) comes in - it can take one SSH session and turn it into whatever configuration you want. Not only can you create windows (tabs) for everything you need, but you can view multiple at once by splitting your terminal into panes, and even have multiple tmux sessions for each project.

Tmux is already installed on Nest, so you can start using it just by running tmux. You should now see your terminal as normal, but with a green status bar at the bottom. You're now inside a tmux session.

Getting started with tmux

Let's try to first create some windows, so we can have multiple things running at once. Every tmux command that you run while inside a tmux session starts with Ctrl + B - this tells tmux that you want to do something with your session. Go ahead and press Ctrl + B, then release those keys and press c (c for Create window). You should still see your terminal - but if you look at the green status bar, you should see that there's a new item: 1:bash*. Let's break this down: The 1 is the window number, bash is the process currently running in that window, and the * tells you that you're currently viewing that window.

To switch windows, use Ctrl + B p to go to the Previous window and Ctrl + B n to go to the Next window. To close a window (killing whatever process is running in it, use Ctrl + B x.

To detach from a session (but leave it running), use Ctrl + B d. You can always attach to it again with tmux attach. There's a lot more commands and ways you can arrange your tmux layouts that I won't get in to here, but you can see them all at https://tmuxcheatsheet.com/.

Tmux + SystemD

SystemD is cool. Tmux is cool. What if we could combine the two? That's one way you can manage your projects, if you'd like. By having a tmux session for each project, you can access logs, manage interactive apps, and keep your sessions organized. Here's how to do it:

1. Create a script that create a tmux session and runs your app within it. For example, if you have a service named <APP_NAME>, you would create a file named <APP_NAME>.sh in your home directory, and paste the following contents, replacing the placeholders where applicable

#!/bin/bash
cd ~/<APP_DIRECTORY>
tmux new -d -s slackbot
tmux neww -d -t slackbot:1 $'bash --init-file <(echo \'. ~/.bashrc; <STARTUP COMMAND>>\')'

2. Make the script executable by running chmod +x <FILE_NAME> 3. Create the SystemD service file in ~/.config/systemd/user named <APP_NAME>.service with the following contents, replacing the placeholders where applicable

[Unit]
Description=<APP_NAME>
After=network.target

[Service]
Type=oneshot
ExecStart=/home/<USERNAME>/<FILE_NAME>.sh
ExecStop=/usr/bin/tmux kill-session -t <APP_NAME>
RemainAfterExit=yes

[Install]
WantedBy=default.target

4. Reload the SystemD user daemon with systemctl --user daemon-reload 5. Enable the service with systemctl --user enable <APP_NAME> 6. Start the service with systemctl --user start <APP_NAME>

And that's it! Make sure your session is there with tmux ls. If it's there, you should be able to attach to it with tmux attach -t <APP_NAME>!