Matt Jones Tech
  • Blog
  • Video Projects
  • Web Projects
  • Can We Talk About TMUX

    Can We Talk About TMUX

    What the heck is tmux? I gotta be honest, I didn’t really know or care what tmux was or what it could do until I came up against a problem I didn’t have a solution for. If you’ve been reading along, last week I dusted off my unused Raspberry Pi and hooked it up with an LCD display. Quite a fun project, and I’m definitely not done with it, but, it wasn’t too long after the LED display setup that I ran into a problem.

    The Problem

    You see, just to get started and learn my way around the LCD display, I ran a few scripts just to see if I could successfully display anything on the screen. It worked! As I got closer to my original plan for the Pi project, I wanted to be able to SSH into the Pi, write and execute a long-running script (long-running as in… basically forever) and then EXIT my SSH session and have the script still running. Yeah, as you can imagine, after exiting my SSH session, as soon as I was disconnected, my script would stop running as well. How can I solve this?

    Enter TMUX

    After a bit of searching, it would seem there are a handful of different solutions to this problem nohup, screen and of course tmux. I opted for tmux because I’d heard of it before but never really explored it, and lots of people online really seemed to use it with great satisfaction.

    So what does it do?

    tmux is a terminal multiplexer. Basically, it allows you to place a terminal inside your terminal and a terminal inside that terminal and so on. It’s extremely capable and very powerful, and for my particular use case, I’m barely scratching the surface. But the key feature I needed was tmux‘s ability to disconnect from and resume terminal sessions.

    Setup

    In order to use tmux, you’re gonna need it installed. Great news, it only needs to be installed on your server or the device you’re SSH-ing into. To install it, on Ubuntu/Debian, it’s as easy as sudo apt install tmux. If you’re using a different system, there’s tons of installation methods, and the whole project is on GitHub, so feel free to check it out

    Basics

    Once it’s installed, you can simply run it with tmux. It may not look like much happened, but once you see a little green bar appear at the bottom of your screen, that’s it. You’re IN. It’s just like a regular terminal session, you can do whatever you want (like execute long running scripts, ping google forever, count to a million, whatever). Once your terminal is going, you can detach from that session by hitting the default prefix hotkey CTRL+B (Kinda the Vim-equivalent of ESC, it won’t look like anything happened, but it puts you in “command mode” where tmux is essentially awaiting instructions).

    After you’ve hit CTRL+B, you can detach from the active session with d. That’s it. Done. Now that you’re detached from your tmux session, you can exit the shell (disconnect from your SSH session) and that’s it. You’re free. Go for a walk, drive somewhere, go eat lunch, take a break, whatever.

    Reconnecting

    Then, some time later, let’s say you want to reconnect to that tmux session you started a while ago. Just SSH into that machine running your process (it can even been from a different device, location, or IP!) and you can reattach yourself to the last (most recently run) tmux session with tmux a which is short for tmux attach.

    And boom. That’s it. You’ve reconnected to the terminal session you started some time earlier, you can kill the process, make updates, do whatever you need to do.

    matt

    September 23, 2024
    Linux, Raspberry Pi, Ubuntu

Prove all things; hold fast that which is good. 1 Thess 5:21