Terminal applications come in many shapes and sizes. Some support tabs, others support splits. Some support both while some of the more archaic ones don’t support either. Terminal multiplexers, like
tmux, allow you to bring the power of multiple sessions as well as persistence to any terminal.
screen command is usually installed by default by hosting providers, but may not be pre-installed on your local system.
tmux command is a more modern replacement for
screen, which also means it may not be as readily available, especially on older systems.
Fortunately, on modern Unix-like system like macOS and Linux, both commands are readily available for installation from the more popular package managers.
This article discusses the basics of starting, detaching and reconnecting to remote sessions. You can perform all of the commands locally if so desired, but the power of persistent sessions really shines when doing so remotely.
It’s also worth noting that active sessions will not persist after a reboot.
🐊 Alligator.io recommends ⤵⚛️ Fullstack Advanced React & GraphQL by Wes Bos
Starting a Session
To start a session, simply type the following:
# Start a screen session $ screen # Start a tmux session $ tmux
You should be taken to a brand new shell prompt. If you’re using
screen you may have gotten a splash screen which requires an extra key stroke.
This may not seem like much, but if you were connected to a remote server and were to close out of your terminal, the session would stay alive.
Persistent shell sessions come in quite handy when you have long running scripts that you don’t want to throw to the background.
If you’ve ever run a Minecraft server, there’s a good chance you’re already familiar with
screen for this purpose.
Detaching From a Session
Closing your terminal, while an easy way to detach from a session, isn’t always preferred, especially if you want to stay connected to the machine while something else runs in the background.
To detach from an active session, you will need to hit a sequence of keys to tell the multiplexer you’d like to detach:
# Detach from a screen session CTRL-a + d # Detach from a tmux session CTRL-b + d
Once you detach from your session, you will be returned to the shell from whence you came. At this point, you can completely disconnect from your remote server if you wanted to.
Now that we’re detached, we will need to know the identifier of the session we’re like to reattach to. If you happened to have disconnected from your remote server, you’ll want to reconnect to the server as well.
To retrieve a list of the sessions currently running, run the following:
# Listing screen sessions $ screen -ls # Listing tmux sessions $ tmux ls
Each will spit out information about it’s respective sessions:
# Output from screen There are screens on: 31188.pts-0.alligatorio (07/02/2019 07:47:57 PM) (Detached) 31166.pts-0.alligatorio (07/02/2019 07:47:51 PM) (Detached) 2 Sockets in /var/run/screen/S-josh. # Output from tmux 0: 1 windows (created Tue Jul 2 19:48:01 2019) [80x23] 1: 1 windows (created Tue Jul 2 19:48:31 2019) [191x77]
Reconnecting to a Session
Remember that output we just saw? We’re going to use the information from that to reconnect to a specific session.
screen uses the process ID as it’s unique identifier, while
tmux simply numbers the sessions as they are created. To reconnect you can run the following:
# To reattach to a screen session $ screen -R 31166 # To reattach to a tmux session $ tmux attach -t 0
We’ll jump back in exactly where the session has last left off. If you had a long running process dumping information out, all of that would be available.
Exiting a Session
Exiting a session is pretty easy. Once you’re reattached, simply stop any commands you may have running, and type in
exit to exit out of the shell and return to your previous (non-multiplexer) shell.
exit again to disconnect from your remote machine.
Beyond the Basics
This article covered the basics of using a terminal multiplexer, specifically to create persistent sessions on remote servers.
Like many of the simple yet power command-line commands out there, these commands have a lot more to offer, such as the aforementioned splitting and tabbing of a terminal.
I personally use
screen locally every day to manage terminals between the projects I am working on. In fact, this post was composed in the
vim editor inside of a
As always, if you’re looking to go beyond the basics, your first stop should be the
man page for the command(s).