tmux (Terminal Multiplexer) is a program that you can use to have a detachable terminal session on your VPS. This means you can login, execute some commands, and then disconnect, returning hours or days later and connecting to the same session. This is different than just logging out.
You’re probably familiar with running commands in the background, such as this:
# some-long-process &
If you’re a little more sophisticated, you might try this:
# nohup some-long-process &
That way your background process won’t be terminated if you disconnect. ‘nohup’ means ‘Ignore the HUP signal’, which is what is typically generated by your shell disconnecting.
But what if you have a complex session going with many windows? Maybe you’re deep into some project and have a half-dozen files you’re editing, some status output flowing, and another window with an error message you’re looking at. Or what if you simply want the convenience of picking up a session exactly where you left off? Enter tmux.
Installing tmux
Getting tmux installed on Debian is as easy as
apt-get install tmux
Using tmux
You could just type “tmux” to start a session, but it’s helpful to give them some kind of label. Let’s call our first session something clever like…”first”:
tmux new -s first
You’ll see a screen like this:
Let’s fire up a command that will generate some output.
while [ 1 ] ; do date ; sleep 30 ; done
Now we’ll detach from the session. To do this, we need an out-of-band command, one that isn’t interpreted by the shell. By default this is Control-b, followed by the letter d. Hit control-b and then hit d, and you’ll be back at your original shell, with the message:
[detached (from session first)]
Now let’s start a second session:
tmux new -s editing
I’ll start editing a document in vim:
Now detach (control-b, d). You can even logoff and login again if you want.
At this point you have two sessions running. You can list your tmux sessions with the tmux ‘ls’ command:
root@debian1:~# tmux ls editing: 1 windows (created Fri May 8 23:13:02 2020) [113x39] first: 1 windows (created Fri May 8 22:58:13 2020) [113x39]
Let’s reattach to our first session. We do this by:
tmux attach -t first
And we see it’s still running:
control-b, d to detach, and then go look at your other session:
tmux attach -t editing
And I’m right back where I was, editing.
How do you get rid of sessions? You can enter ‘exit’ in a session (for example, I could quit or save-quit my vim session, and then type exit). If you want to kill a session without returning to it, use tmux’s kill-session command:
root@debian1:~# tmux ls editing: 1 windows (created Fri May 8 23:13:02 2020) [113x39] first: 1 windows (created Fri May 8 22:58:13 2020) [113x39] root@debian1:~# tmux kill-session -t first root@debian1:~# tmux ls editing: 1 windows (created Fri May 8 23:13:02 2020) [113x39] root@debian1:~#
Advanced tmux
Besides just allowing you to detach from and then reattach to sessions, you can also use tmux to carve up your window real estate.
I’m going to start a new session:
tmux new -s panes
Of course, I could call it anything. Now I’ll type the following (don’t type the commas):
control-b, % control-b, "
Here’s my screen now:
The first command (control-b, %) splits the current pane vertically, while the second (control-b, “) splits it horizontally.
I can move between these panes by typing control-b, o. Or I can jump to specific window with control-b, # where “#” is the pane number I want to move to. Panes are numbered in the order created. You can type control-b, q and tmux will briefly display the number of each pane:
Type “exit” inside any pane to close that pane. And you can type control-b, d to detach from the entire pane set and then reattach with “tmux attach”.
There’s much more you can do with tmux, but we’ve covered the most useful commands. If you’re interested in diving deeper, I recommend Brian Hogan’s book, “tmux: Productive Mouse-Free Development”.
Related Posts:
- WHMCS and cPanel Prices Going Up By… 5%? 10%? Keep Guessing… - October 9, 2024
- Has the Biggest Performance Bottleneck in Python Finally Been Slain? - October 8, 2024
- Nuyek’s Spooktacular Halloween Sale: Don’t Miss These Quarterly Deal Treats!Plus Dedicated Servers Starting at Only $18/Month! - October 7, 2024
Leave a Reply