Close Terminal without Killing Running Processes on Linux with Tmux

C

When connecting remotely to a Linux server, all the processes or scripts started by you through the terminal run as long as you are connected to the server. As soon as you close the terminal, the processes started by you will get terminated. 

I faced this issue when I was new to Linux servers. The naive me ran scripts through cronjobs and the issue was temporarily sorted out for me. It did not take long until I realized running scripts through cronjobs wasn’t feasible for everything. Eventually, I needed to run scripts that took hours to complete but needed to run on demand. So scheduling the script to run with cronjobs was not useful here.

Close SSH Terminal without Killing Running Processes with Tmux linux

After spending some time looking for a solution, I came across Tmux A very useful Linux package that enables users to run multiple virtual terminals on Linux. Tmux allows Linux users to run multiple terminal sessions and run separate processes on each of them. The user can easily switch between these terminals by detaching from one terminal session and attaching to another. 

Thanks to Tmux I was able to run scripts and close the terminal without having to worry about the scripts being terminated. After reconnecting back to the server, I just have to connect to the specific tmux session and I can see how far the process has progressed. Tmux helped me save electricity costs which would have been wasted unnecessarily leaving the computer on while the script completed its job.

Close SSH Terminal without Killing Running Processes with Tmux

For all those who want to Close SSH Terminal without Killing Running Processes, here’s how you can do it with Tmux.

Tmux is compatible with most Linux operating systems. Since I am using Ubuntu, this guide will be demonstrated on the same operating system.

Here are the commands to install tmux on the respective operating systems:

PlatformInstall Command
Arch Linuxpacman -S tmux
Debian or Ubuntuapt install tmux
Fedoradnf install tmux
RHEL or CentOSyum install tmux
macOS (using Homebrewbrew install tmux
macOS (using MacPorts)port install tmux
openSUSEzypper install tmux
Installing tmux on Ubuntu.
Installing tmux on Ubuntu.

Once installed, you can start a tmux session by entering tmux or tmux new in the terminal. 

tmux new

Each instance of tmux is called session. Tmux sessions can be managed by both shortcut keys or commands. Shortcuts can come very useful when there are programs running in the tmux sessions. By default, the tmux sessions will be named as numbers starting from 0. You can name an instance by setting a value for the -s flag.

tmux new -s mysession
tmux new -s mysession

You can detach from a tmux instance by pressing the Ctrl + B D keys.

detaching from tmux session

To view a list of all running tmux sessions you can use one of the following commands.

tmux ls
tmux list-sessions
listing all tmux sessions

You can easily attach to any of the sessions by typing the following command with the tmux instance number.

tmux attach -t mysession

Session can killed with the following commands

tmux kill-ses -t mysession

For more information on tmux’s features, check out the Getting Started page or the handy tmuxcheatsheet website.

With tmux, you can now close SSH Terminal without worrying about the running processes being killed on linux.

I recall many situtations when my internet connection got disconnected and thanks to tmux, the script kept running on this server. Without tmux, the terminal would have closed and so would have been the running process. Another great about tmux is, multiple users can access the same tmux session and check the progress of the script, which wouldn’t not have been possible using the terminal on linux.

About the author

lovejeet

Add Comment

By lovejeet

lovejeet

Get in touch

Quickly communicate covalent niche markets for maintainable sources. Collaboratively harness resource sucking experiences whereas cost effective meta-services.