tmux is an extremely useful command line utility. It’s a screen multiplexer much like the screen utility. It basically allows you to have as many command line sessions as you like. This is useful for processes and programs that run for a long time rather than using the CNTRL-Z/bg/fg commands to shunt a process into the background. The problem with this is that whilst your process or program might still be running in the background, once you disconnect from your logged in session, the process is inaccessible, meaning that you cannot pull that process back into the foreground with it’s process number (PID). bg/fg commands use a separate number system linked to your current user session. If you log out of that session, even if the process belongs to your user, you cannot pull it back into the foreground. This is whee I’ve found tmux to be most useful because it allows many sessions and as long as these sessions belong to your user, you can retrieve them from any SSH/command line session.
I’m not going to go into everything tmux can do, simply the easy stuff that I find makes it so useful.
1. Install tmux.
To install tmux on Ubuntu/Debian based Linux systems, use: –
sudo apt-get install tmux
If you’re using a Red-Hat based system use: –
yum install tmux
2. Create a tmux session.
To create a tmux session from the command line use: –
tmux new -s session_name
..where session_name is the string name you want to name the session. You don’t have to use named sessions, tmux will automatically number them which you can use to access a detached session, but I find named sessions easier and clearer to work with.
Once you’re in a session, you’ll find it looks very much like a typical SSH/command line session aside from a green bar menu at the bottom of the screen. If you want to quit this session, you can simply use “exit” like you would in any typical command line session. However, if you have something running in this session and you want to logout and come back to it later, you should detach the session and leave it running in the background.
3. Detach a running tmux session.
To detach a running session, hold down CNTRL-B and then press ‘d’. The CNTRL-B key combination is known as the prefix shortcut and you’ll need to use this key combination to access commands in tmux without disturbing the shell session you’re in. The D key in this instance detaches the session and leaves it running in the background. If your process isn’t doing anything and you have access to the session command line you can also use: –
4. List active tmux sessions.
From the command line, you can see how many tmux sessions you have running by simply running:-
This will list the running tmux sessions in the shell. From the below, you can see I have one session active which I have called simply “session1”.
session1: 1 windows (created Tue Jan 16 11:28:36 2018) [130x33]
5. Reattaching to a deteached tmux session.
To reattach to this tmux session, you simply have to use: –
tmux attach -t session_name
So if I wanted to attach to the “session1” session above, I would use: –
tmux attach -t session1
…and you’re back in the tmux session shell you created earlier.
6. Switching tmux sessions.
You can also switch from one tmux session to another by using the following command from within a tmux session. So if I had two tmux sessions, I could switch from one shell to the other by using: –
tmux switch -t session_name
7. tmux Windows within sessions.
You can also create multiple shell windows within one session. From within a tmux session, you can use: –
or you can use CNTRL-B + c.
To switch between session windows, simply use CNTRL-B [0-9] where the number is the sequential number of the window or the following if you want to do it from the session command line: –
tmux select-window -t [0-9]
I’ve found this utility extremely useful when it wasn’t practical to use nohup