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.
- 
SSH into the server ssh user@your-server
- 
Start a new tmux session tmux new -s mysessionYouβre now inside a named tmuxsession calledmysession. Everything you do inside this session will continue running even if your SSH connection drops.
- 
Run your long-lived command ./my-long-script.shOr any other long-running task. 
- 
Detach from tmux(leave it running) by pressing:Ctrl + b, then dYouβll be back at the normal shell, and your tmuxsession will continue running in the background.
- 
After reconnecting to the server, you can reconnect to the session: tmux attach -t mysessionYour session will be exactly as you left it. 
To list all tmux sessions:
tmux ls
To kill a session:
tmux kill-session -t mysession
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.
