Introducing Tmux

in #linux5 years ago (edited)

I wrote a post yesterday about PM2 and how it is one of my favorite apps. While the majority of my scripts and bots are managed by PM2 there are situations where I use Tmux instead.

Tmux is a more modern version of screen. While it is a lot better it does lack on native feature screen has, logging. I'll talk about this later and options to fix this, but outside of that I really prefer tmux.

What is Tmux?

Tmux is a terminal multiplexer, this allows multiple terminal windows in one terminal. More importantly you can run a program or terminal from SSH and then exit SSH and it will be there when you get back. Although it does more than that, that is the main purpose of tmux.

If you want to know the differences between tmux & screen, I suggest reading this article.

The main advantage of screen is the ability to have multiple users connected to the same screen session and built-in logging, although this isn't mentioned in the above article.

How to use tmux

Tmux should be installed on most Linux operating systems but if it is not, just install it using sudo apt install tmux or whatever command for your distribution's package manager.

Using tmux is very easy, to start a session just type in tmux. If you are going to be running multiple sessions, you can name your session by using the command tmux new -s mysession instead of just typing `tmux. This will allow you to reattach to the session directly by name.

You can exit ssh at any point and your session will remain how it was. Either as an interactive session or running a process. Later you can reconnect to your unnamed session by using tmux attach or tmux attach -t mysession for a named session. You will be exactly where you were when you left, although keep in mind any programs running may have changed or even crashed since you last logged in.

If you want to see the list of sessions running use tmux ls as this will show all sessions running. You can then attach to the session by name or id.

In the rare situation, you need to kill a session, you can kill it with tmux kill-session -t mysession, I rarely ever have to do this and you can just attach and exit from there if you want as well.

While I won't go into detail on how to use windows and panes, I will mention they are available a brief introduction. If you look at the gif at the start of this post you can see windows and panes in action. They allow you to split a terminal and run multiple terminals.

A window acts as a tab (virtual desktop) where a pane acts as a visual split in your terminal.

The default prefix is control-b which is kind of awkward as it is very close to control-c that cancels a process. I highly recommend changing this prefix, which can be changed in the tmux configuration file. I am going to refer to the default control-b as prefix from this point on.

To open a new window you use prefix-c and to open a pane you will use prefix % for a horizontal split and " for a vertical split.

Full list of commands for windows and panes:
(from the Tmux Cheatsheet)

If you want to detach from a session to return to your terminal while keeping the session running you will need to use prefix-d.

You can also enable mouse support for tmux if you want to use a mouse to navigate.

Logging

As I said before, screen has native logging and tmux does not. One of the plugins for tmux is tmux-logging that enables the ability to log each pane/window to a file.

You can find details about this plugin here

Anytime I am running something interactively, or actively testing/debugging a process or script, I will use tmux instead of PM2. Once I am comfortable something is ready to be run on its own, I will plug it into PM2. I also use tmux for things like steemd nodes as they cannot be run under PM2 and cannot just be restarted on failure.

Image Source: 1 2

Sort:  

If what you're doing is command line, the standalone script utility can also be used for logging. Sometimes I use this outside of a session manager too,

If you like PM2 for nodejs, a good solution for non-nodejs apps like steemd is Monit:

https://steemit.com/steem/@inertia/using-monit-to-manage-steemd

Pm2 supports python and ruby as well. Only problem I ever had is python buffers the output and you need to specify -u arg with python if you want real time output which can be handled in an eco file.

Otherwise works great.

Tmux is just another terminal no? No autorestart for crashed software like pm2? I use tmux only for a python bot that never crashes, but all my nodejs bots go on pm2 because every time someone does something weird with my bots, there is a high chance that it will die, and I need it up 24/7. Maybe if I implemented some kind of safety never-die measure I could use tmux for nodejs, but so far pm2 has been more than enough.

Also, does tmux have an api accessible from scripts like pm2? pm2's api is a bit bad so i've always been looking for a good process manager that has an api.

Tmux is just another terminal that persists when you quit SSH and allows you to use multiple panes/windows to split it up and have multiple terminals in one.

I only use tmux when something is interactive, under testing, or not a script

You can look at Monit for an alternative to PM2 that has more api functionality.

I’m dreaming one day to start being able to use bots for various purposes as well.
I keep seeing for example how people have big success with them in the mining gambling dApps....

Posted using Partiko iOS

As a still learning programmer loved the tutorial, it's really well explained and I'll use PM2 and tmux when I start building scripts. I do wonder why you didn't use the stem tag? This is the sort of stuff I would love to see on stemgeeks

#technology is also picked up by STEMGeeks.

Did not know, thanks!

The tribe listens for

  • science
  • technology
  • engineering
  • math
  • stem

👍
~Smartsteem Curation Team

can i pls get one 100% upvote from you? 😁