207 words, 2 min read

When working on a remote Linux server over SSH, it's common to run long-running commands like database migrations, backups, builds, or batch processing. One major problem: if your SSH connection drops, so does your command β€” unless you plan ahead.

This is where tmux becomes an invaluable tool.

tmux is a terminal multiplexer. It allows you to start a terminal session, run processes inside it, detach at any time, and later reattach β€” even after disconnecting from SSH. Your command keeps running in the background as if nothing happened.

  1. SSH into the server

    ssh user@your-server
    
  2. Start a new tmux session

    tmux new -s mysession
    

    You’re now inside a named tmux session called mysession. Everything you do inside this session will continue running even if your SSH connection drops.

  3. Run your long-lived command

    ./my-long-script.sh
    

    Or any other long-running task.

  4. Detach from tmux (leave it running) by pressing:

    Ctrl + b, then d
    

    You’ll be back at the normal shell, and your tmux session will continue running in the background.

  5. After reconnecting to the server, you can reconnect to the session:

    tmux attach -t mysession
    

    Your session will be exactly as you left it.

To list all tmux sessions:

tmux ls

To kill a session:

tmux kill-session -t mysession