Basic Unix Tricks Series - #3 tmux workflow
tmux workflow
One of my most used programs is not nessisarily a utility itself but rather a
program for managing running shell scripts, ssh sessions, and any type of bash
shell process. The program most popularly used for this is tmux, which is now
often shipping on linux operating systems like the latest Ubuntu Desktop.
If tmux isn't installed on your machine it can be installed with:
sudo apt-get install tmux
Once you start using terminal based applications and processes you will quickly
find you may want to run a process and keep it running without having to keep a
dedicated terminal window open on your user-interface. Traditionally this could
be done with a combination of built in commands, for example:
# The following is not recommended as a means of backgrounding processes
# press ctrl-z to pause the process
bg
# bg command 'backgrounds' the process but it will still be associated with
# the open terminal window, thus closing the window terminates the process
jobs
# the jobs command will return a list of processes running terminal window
# note the ID number of the process you want to disassociate from window
disown -h %1
# replace 1, with the ID of the process you want to disown
The problem with the above is that there isn't an easy way to re-attach to a
disowned process or program.
Instead it is recommended that you use tmux to manage and switch between
virtual terminal windows.
To create a new terminal session simply run
tmux
You should see a small green bar at the bottom of the page which indicates your
window is a tmux session. Now as an example program run the following:
top
You should now see top
running and listing all the running processes on your
machine. Now we will detach from the tmux session and keep the window running in
the background on the operating system, so we can easily re-attach later. To do
this press ctrl+b (at the same time) followed by d.
You will now be back at your default terminal window, which can be closed
without effecting the tmux-session running in the background.
If you want to see all the tmux-sessions running on your computer run:
tmux ls
This will return a list of tmux-sessions and their associated IDs. To re-connect
to one of these sessions run the following (replacing 1 with the ID of the
session you want to attach to):
tmux a -t 0
This can be useful in several situations including but not limited to:
- You want to connect to multiple servers or IoT devices at once to run
commands which may take some time to complete. - You want to background a command but keep the ability to return to debug.
- You're using a terminal based editor like vim and want to switch between
multiple files without closing them.
tmux is highly customizable and has many quick keyboard shortcuts. Most of the
keyboard shortcuts will use ctrl+b followed by a letter. One of my most used
keyboard shortcuts is ctrl+b followed by s. In this case s
stands for switch
and allows you to quickly switch between open tmux sessions without detaching.
For example say I have a few ssh sessions open and want to switch from one to
another while I wait for a process to complete. I simply run ctrl+b s, and use
the down arrow to select the session I want to open. I then run whatever command
I want on another session and quickly press ctrl+b s again to switch back.
Currently I use screen, but it seems tmux may be better https://superuser.com/questions/236158/tmux-vs-screen
Posted using Partiko Android
Meh. Sounds like screen is still king from the article. The cons for
screen
aren't much to write home about and the pros fortmux
aren't all that great. The best part is the vim-ish/emacs-ish bindings, but only on the surface. Those are editor bindings which actually make tmux less intuitive since people familiar consoles and terminals find the shortcuts remapped. Screen is better at this because the bindings are still very similar while also preventing pitfalls like accidentally freezing flow control.Another one of the pros is scriptability which is another point to it being non-intuitive. The intuitive tool for scripting wouldn't be
tmux
. It would actually benohup
. Why usetmux
whennohup
is already so awesome for scripting? Plus, it's ubiquitous and intuitive. Try to find another shell/platform wherenohup
isn't already prevalent.Just to throw one more log on the fire: one of the biggest pros for
screen
is stability becausetmux
isn't the most stable thing out there. That's not the environment you want to run your scripts in.