Introducing Tmux

in #linux5 years ago (edited)

<p dir="auto">I wrote a post yesterday about <a href="https://steemit.com/linux/@themarkymark/how-to-use-pm2-to-manage-your-scripts" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">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. <p dir="auto">Tmux is a more modern version of <strong>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. <h1>What is Tmux? <p dir="auto">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. <p dir="auto">If you want to know the differences between tmux & screen, I suggest reading this <a href="https://www.slant.co/versus/11858/11859/~tmux_vs_screen" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">article. <p dir="auto">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. <h1>How to use tmux <p dir="auto">Tmux should be installed on most Linux operating systems but if it is not, just install it using <code>sudo apt install tmux or whatever command for your distribution's package manager. <p dir="auto">Using tmux is very easy, to start a session just type in <code>tmux. If you are going to be running multiple sessions, you can name your session by using the command <code>tmux new -s mysession instead of just typing `tmux. This will allow you to reattach to the session directly by name. <p dir="auto">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 <code>tmux attach or <code>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. <p dir="auto">If you want to see the list of sessions running use <code>tmux ls as this will show all sessions running. You can then attach to the session by name or id. <p dir="auto">In the rare situation, you need to kill a session, you can kill it with <code>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. <p dir="auto">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. <p dir="auto">A window acts as a tab (virtual desktop) where a pane acts as a visual split in your terminal. <p dir="auto">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 <strong>prefix from this point on. <p dir="auto">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. <p dir="auto">Full list of commands for windows and panes:<br /> (from the <a href="https://gist.github.com/henrik/1967800" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Tmux Cheatsheet) <p dir="auto"><span><img src="https://images.hive.blog/768x0/https://i.imgur.com/xlyl2kJ.png" srcset="https://images.hive.blog/768x0/https://i.imgur.com/xlyl2kJ.png 1x, https://images.hive.blog/1536x0/https://i.imgur.com/xlyl2kJ.png 2x" /> <p dir="auto">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. <p dir="auto">You can also enable mouse support for tmux if you want to use a mouse to navigate. <h1>Logging <p dir="auto">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. <p dir="auto">You can find details about this plugin <a href="https://github.com/tmux-plugins/tmux-logging" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">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. <p dir="auto"><sub>Image Source: <a href="https://msadowski.github.io/two-months-of-terminal" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">1 <a href="https://gist.github.com/henrik/1967800" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">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? 😁