Monitor Minecraft servers with Tmux

By Rob Zwetsloot. Posted

Connect, disconnect, and reconnect as often as you like to your remote server activities without losing anything when the terminal closes

With tmux, a popular terminal multiplexer, you don’t have to lose any work when disconnecting from your Raspberry Pi in the terminal. In fact, if you close your terminal window, change network connections, or even close the lid on your laptop, tmux will keep running on the remote machine with your long-running programs inside it. None of these otherwise killer disconnections will interfere with the smooth running of your remote programs. They’ll be able to keep chugging along without you until you get back to them at your convenience. Any long-running task will do, such as a Minetest server.

You'll need

  • A Minetest or Minecraft server
  • SSH connectivity to the server
  • Access to install tmux there

STEP-01 Installing tmux

If tmux is not already on the Raspberry Pi, now is the time to add it.

sudo apt-get update;
sudo apt-get install tmux;

That will provide both tmux and its manual page. Under the hood, tmux operates on a client-server model with each running as an independent process. The part you see is the client. The server is the part that stays running even when the client is disconnected. Customisations, such as key bindings or changing the status bar, take place in the configuration file, ~/.tmux.conf, but for now we’ll head straight into running tmux, starting with the basics.

STEP-02 Starting an initial tmux session

Log in to your server host computer using SSH. At the regular shell prompt, before you do anything else, start a new tmux session:

tmux new-session;

While tmux is running, you’ll see a status bar across the bottom of the terminal. Over on the right-hand side it will show the machine name, time, and date. On the left side, you’ll see the tmux session number in brackets and next to that the number of the first tmux window and the active program name, such as the shell ‘bash’. The asterisk means that particular tmux window is the one in view at the moment.

STEP-03 Disconnecting from tmux

Start your game server there inside tmux, like you would in a normal shell. Once you have it running in tmux, disconnect by closing the terminal window. This closes your SSH session. Normally the remote processes would shut down when that happens. It will close the tmux client to be sure. However, your remote program is still running comfortably inside the server component of tmux and you can reconnect to it whenever you want, when you log in again with SSH and launch a tmux client.

STEP-04 Reconnecting to tmux

Log in again using SSH. At the regular shell prompt, optionally check for your existing tmux session:

tmux list-sessions;

That will show your established session, when it was started, and how many tmux windows it may have running. However, whether you check first or not, reconnecting is easy.

tmux attach-session;

You’ll find that tmux then rejoins your session progress. If things were active while you were gone, some new text might be there and the old text may have scrolled off the top of the screen.
You can disconnect and reconnect as many times as you like: there are no limits there.

STEP-05 Quitting tmux

Your tmux session ends when its last tmux window is closed. So quit what you had running inside tmux, whether it was your game server or another program, as normal. Then type exit at the shell prompt inside tmux to quit that tmux window. When the last tmux window has been closed, the tmux server to which it’s connected will also quit. It will leave you in your regular shell, still logged in via SSH. If you want to be sure, then try listing the tmux sessions:

tmux list-sessions

…and that will let you know whether tmux is still there or not.

STEP-06 Going further

Steps 1 through 5 were just the tip of the iceberg. The tmux reference manual covers much more and is well worth becoming familiar with, even if it’s a bit daunting at first.
Over time it will become comfortable and you will have a fun, easy, and powerful tool with which to augment your terminal. So before you log out from SSH, check the reference manual briefly:

man tmux

Look up some extra options for new-session, attach-session, and list-sessions there. You’ll get a good list of words and phrases to help find the many specialised tmux tutorials via your favourite search engine.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.