It is always a good practice to run a long running process inside a Tmux session when working with remote systems via SSH. Because, it prevents you from losing the control of the running process when the network connection suddenly drops. Just in case the network connection gets dropped for any reason, the processes inside the Tmux session will keep running on the remote systems, so you can re-attach to the Tmux session using "tmux attach" command once the network connection is back online. What if you forgot to start the Tmux session in the first place? No matter how careful you're, sometimes you may forget to start Tmux session. Here is a simple way to avoid this problem. You can autostart Tmux session on the remote systems when logging via SSH. This is especially helpful if you lost the network connection when upgrading a remote Linux server via SSH from your local system. Please note that the following tip will automatically start a Tmux session whenever you try to log in to your Linux system either locally or remotely via SSH from another system.
Autostart Tmux Session On Remote System When Logging In Via SSH
To autostart Tmux session when connecting via SSH, edit your remote system's ~/.bash_profile file:
$ nano ~/.bash_profile
If the file is not available, just create it.
And add the following lines in it:
if [ -z "$TMUX" ]; then tmux attach -t default || tmux new -s default fi
Save and close the file. Log out and log back into the remote systems. You will be landed into a new Tmux session named 'default'.
Now, start any process inside the Tmux session and detach it from by pressing Ctrl+b followed by d. The process will keep running inside the Tmux session even after the network connection is dropped. Once the network connect is back, SSH into the remote system and you will see that the job is still running!!
Have a look at the following animated output:
Let me clarify what I just did in the above output.
- I connect to my remote Ubuntu server via SSH from my local system. A new Tmux session named "default" is automatically started.
- Inside the Tmux session, I run a command (Ex. watch grep \"cpu MHz\" /proc/cpuinfo) to monitor the CPU speed.
- Then, I detached from the session by pressing Ctrl+b and d.
- And then, I list the Tmux sessions using "tmux ls" command and exit the SSH session.
- Finally, I reconnect to the remote system via SSH and verify if the process is keep running inside the Tmux session without any interruption.
Hope you understand.
To stop auto-starting Tmux session, simply delete the lines that added earlier from the your remote system's ~/.bashrc or ~/.bash_profile file.
This simple tip helps you to avoid losing the control of your remote jobs. You don't always have to manually start Tmux every time. Just make Tmux to start automatically whenever you SSH into a remote system as described in this guide.
Suggested read:
- How To Autostart Screen Session On Linux When Logging In
- Reptyr – Move A Running Process From One Terminal To Another Without Closing It
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Reddit | Facebook | Twitter | LinkedIn | RSS feeds
Have a Good day!!
2 comments
What if I don’t Loggin to the remote device? tmux will make the reconnection if I don’t loggin or not?
Is mandatory the ssh login in order to reboot tmux?
If you don’t login, tmux won’t start.