| by Arround The Web | No comments

Managing tmux Sessions

Working remotely on different machines can become overwhelming without using a program like tmux. In addition, having to keep track of what work you are doing on each machine while trying to remember what place you were at in each terminal window for each machine can quickly get out of hand.

However, with a program such as tmux, this overhead of developer energy and time is removed completely. Furthermore, with tmux, you can not only work with multiple machines and multiple environments at the same time, but you can also quickly switch between them. Therefore, learning how to better handle tmux sessions can potentially boosting your productivity and your ability to work.

Listing tmux Sessions

To show a list of all the tmux sessions currently running on a machine, the command tmux ls is used in the terminal, as shown below:

Information given by tmux ls command appears in the following order:

  • website1, which is the name of the tmux session listed here,
  • 2 windows, which shows how many windows are in the website1 tmux sessions, and
  • (created Thu Jun 9 03:08:42 2022) which shows the date the session was created.

If a machine does not have any tmux sessions running when you run tmux ls command, the following output is shown:

Renaming tmux Sessions

Like many other operations for tmux, renaming a session in tmux can be done in one of two ways. The first way to rename a session in tmux is to do it from outside of the session, in the terminal. Use the following command to rename a session:

$ tmux rename-session -t <old name> <new name>

For example, in the screenshot below, a tmux session is currently running, called webdev, which we want to rename as website1.

If the renaming operation is successful, the command does not produce anything, and we can check the new name of the session through $ tmux ls as shown:

The second way to rename a tmux session is from within the session. Similar to the example above, we are going to rename the following session from webdev to website1:

The key combination Prefix, $ is used to achieve that. For example, in my Ubuntu installation, this key combination would be Ctrl + b, $.

The result is shown in the screenshot. As you can see, the session is now named website1 in the bottom left corner of tmux:

Creating a New tmux Session with a Name

Similarly, if you need to create a tmux session with a name beforehand, instead of renaming it later, you can use the following syntax for tmux new command:

$ tmux new -s <name of your tmux session>

For example, if you want to create another session where you are setting up another remote server for VPN use, you can create a tmux session called vpnserver by using the following command:

$ tmux new -s vpnserver

This new tmux session is shown below:

Attaching to tmux Sessions

When you want to attach your terminal to a currently running tmux session, the command to use is:

$ tmux attach -t <session name>

For example, if you want to continue your work on the website1 session and currently your terminal is not attached to that specific tmux session, you run the following command:

$ tmux attach -t website1

To get a list of sessions you can attach to, use the ls command as shown in a section above.

Detaching from tmux Sessions

Detaching from tmux requires using the command Prefix + d. For example, in my Ubuntu installation, the prefix command is Ctrl + b, so to detach from the currently running session, I need to press Ctrl + b and then press d to detach. Once you’ve detached your terminal from the tmux session, the session is still running. This means that any processes you’ve left running in the session, such as htop, a file download through curl, or any batch command, will continue to run until completed.

This function of tmux works well for remote servers where you need to ssh to initiate a process and then let it run in the background. Unlike directly running commanding from your terminal through ssh, using tmux allows you to use detach command to run any process in the background.

Killing tmux Sessions

Killing a tmux session destroys it completely. Therefore, this command should be used with care. This command is not reversible and can lead to a loss of work.

Similar to renaming a tmux session, there are two ways to kill an individual session. The first way is through the terminal, and the second is from within the tmux session.

In order to kill a tmux session from the terminal, use the following command:

$ tmux kill-session -t <session name>

For example, if I have a tmux session running called vpnserver and I want to kill it through the terminal, I can use the following command:

$ tmux kill-session -t vpnserver

As shown below, the session is no longer available and is destroyed.

The second way of killing a tmux session is from within the session. Simply exit out of all the panes and windows running in that session and exit out of the last window of the session. This will cause the session to be killed, just as it would have been through the terminal. The screenshot below shows a running session of tmux called vpnserver:

When we attach our terminal to this session and exit out of all of the windows, the session is killed. This can be seen in the screenshots below:

After exiting all the windows of the vpnserver session, tmux ls shows that the session has been killed:

Killing All tmux Sessions on a Machine

Finally, if you need to kill all the tmux sessions on a machine, you can use the following command:

$ tmux kill-server

As shown in the screenshot below, two tmux sessions are running, webdev and vpnserver. If you run the kill-server command, both sessions are killed:

In a nutshell:

To start a tmux session, use the following command:

$ tmux new -s <session name>

To detach from the current tmux session, use the following tmux key combination:

Ctrl + b d

To reattach to a tmux session, use the following command:

$ tmux attach -t <session name>

To rename a tmux session, use the following command:

$ tmux rename-session -t <old session name> <new session name>

You can also rename the currently running tmux session using the following tmux key combination:

Ctrl + b ,

Furthermore, you can use the following command to kill a tmux session:

$ tmux kill-session -t <session name>
Share Button

Source: linuxhint.com

Leave a Reply