How to keep persistent SSH session using TMUX

Ever wonder how to keep a program installer or OS updater running on your Raspberry Pi, without staring at the black screen waiting for it to finish. Your precious time should be use for something worthwhile, like playing game or doing something else on your laptop and being able to disconnect without killing the running session in the background.

Supposed you have installed tmux, if not run this on your Pi via SSH:

sudo apt-get update

sudo apt install tmux

Edit your ~/.bashrc, append the following at the bottom:

if [ $(tty) == /dev/tty1 ]; then
/usr/bin/tmux new -s sessionname
/usr/bin/tmux attach -t sessionname
fi

When you log into your profile, you can attach to the existing session, so add the following to your ~/.profile file:

/usr/bin/tmux attach -t sessioname

This will ensure that you will log back into the same session in your next ssh session after getting disconnected.

Usin the same logic you can also run other programs within tmux by editting the two files.

 

 

(Visited 375 times, 1 visits today)