Tuesday, March 21, 2017

OMG tmux how have I ever lived without you!

How the **** I have I never heard of/used "tmux" on a Linux box?  I mean seriously, they had me at Control + B --> D.  For those like me who had never heard of "tmux" it is a NECESSITY if you ever run any long running (or even short running) scripts that require you to be connected to a SSH session.

Let's pretend that you are upgrading a very important application on your server and this upgrade script does thousands of instructions and database queries etc etc.  Now let's pretend that you are 4 hours in and all of a sudden a squirrel nibbles the fiber optic cable connecting you to the interwebs (yes, this happened).  All of a sudden all of the processes that are running have been terminated because your SSH session was disconnected.  Now you have to pray that it either finished, somehow is still running, and didn't corrupt itself and you have to roll back to a snapshot (you did make a snapshot before you started right?).

Now let's pretend you knew about "tmux" and let's run though this situation again.  You first SSH into your server and then you immediately issue the command:
Now you get a nifty colored bar at the bottom of your SSH screen.  Mine is green, but it will probably depend on your terminal settings.  You seem to get a standard prompt like you had before.  That green bar is the only thing that really lets you know you are in tmux.  Now you issue your script commands.  4 hours in the rascally squirrel gets his teeth into your cables.  After flipping out and finally realizing what happened you then proceed to go to your mom's house for some nice good old fashioned cable internet and cookies.

From there you connect to your server (VPN + SSH) and then you issue the command (as the SAME user that you ran tmux from before):
tmux attach

Now you are brought right back where you were!!! HAPPINESS!!!

Useless example:
$ tmux
Now in tmux:
$ nano hello.txt
If you purposely want to disconnect hold down the CONTROL key and press "b" then press "d" to "detach"
Close your SSH connection completely or go to another computer and connect to your server.  Now as the same user type:
$ tmux attach
Violoa! you now are editing hello.txt!
To exit from tmux you can just type "exit" at the prompt.  Here is a nifty site dedicated to more tmux use cases like splitting screens etc: https://danielmiessler.com/study/tmux/

Happy tmux-ing!

No comments: