How to Use tmux to Keep Processes Running After SSH Disconnect - Entexion Hosting Solutions

How to Use tmux to Keep Processes Running After SSH Disconnect

When working on remote servers via SSH, one of the most frustrating things is losing your connection—and with it, the long-running process you just kicked off. Enter tmux, a powerful terminal multiplexer that allows you to run, disconnect, and resume terminal sessions at will. In this guide, you’ll learn how to install tmux and use it to keep your processes running, even after you log out of your SSH session.

🔧 What is tmux?

tmux (Terminal MUltipleXer) lets you manage multiple terminal sessions from a single window. It’s particularly handy when you need to:

  • Run long scripts or processes

  • Detach and reattach to sessions

  • Work in multiple terminal windows without using screen or multiple SSH sessions

🛠️ Installing tmux

On Ubuntu/Debian:

bash
sudo apt update sudo apt install tmux

On CentOS/RHEL:

bash
sudo yum install tmux

or for newer versions using dnf:

bash
sudo dnf install tmux

On macOS (with Homebrew):

bash
brew install tmux

On Arch Linux:

bash
sudo pacman -S tmux

Once installed, check the version:

bash
tmux -V

🚀 Starting and Using tmux

Start a new session:

bash
tmux

Or give your session a name (recommended):

bash
tmux new -s mysession

This opens a new terminal session within tmux.

🏃 Run Your Process Inside tmux

Now that you’re in a tmux session, run your command or script as usual:

bash
python myscript.py

Even if this is a long-running process, you don’t need to worry about staying connected.

🔚 Detach from a Session

To safely detach from the session and return to your regular shell:

  1. Press Ctrl + B, then release both keys.

  2. Press D.

You’ll see a message like:

pgsql
[detached (from session mysession)]

You can now safely close your SSH session. The process will continue running in the background.

🔁 Reattach to a Session

Later, when you SSH back into the server, you can reattach to your session:

bash
tmux attach -t mysession

If you forget the name of the session:

bash
tmux ls

This will list all active tmux sessions.

❌ Kill a Session

When your task is done, you can kill the session:

bash
tmux kill-session -t mysession

Or kill all sessions (be careful!):

bash
tmux kill-server

💡 Tips and Tricks

  • Split panes: Ctrl + B, then % for vertical or " for horizontal

  • Switch panes: Ctrl + B, then arrow keys

  • Create new window: Ctrl + B, then C

  • Navigate windows: Ctrl + B, then N (next) or P (previous)


✅ Summary

Using tmux is an essential skill for anyone who works on remote servers. It helps you:

  • Keep processes running after SSH disconnect

  • Easily switch between terminal windows and tasks

  • Improve your workflow and system resilience

Once you get comfortable with tmux, you’ll wonder how you ever managed without it.

 

Entexion opens the door to your digital transformation journey

Review us on TrustPilot

Copyright © 2021-2024 GNET Communications Ltd