Home Command line utilities Autostart Tmux Session On Remote System When Logging In Via SSH
Autostart Tmux Session On Remote Linux System When Logging In Via SSH

Autostart Tmux Session On Remote System When Logging In Via SSH

By sk

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

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:

Autostart Tmux Session On Remote System When Logging In Via SSH

Let me clarify what I just did in the above output.

  1. I connect to my remote Ubuntu server via SSH from my local system. A new Tmux session named "default" is automatically started.
  2. Inside the Tmux session, I run a command (Ex. watch grep \"cpu MHz\" /proc/cpuinfo) to monitor the CPU speed.
  3. Then, I detached from the session by pressing Ctrl+b and d.
  4. And then, I list the Tmux sessions using "tmux ls" command and exit the SSH session.
  5. 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:

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More