tmux: Keep Linux Sessions Alive (so you can go back to the same terminal window from anywhere, anytime)

Quick cheat sheet for reference: 

1 Concepts:   Session, window, pane. You can also multiple tmux sessions, each of them can have multiple window, and each window can have multiple panes.
1 tmux new -s work     # create a new session
1 Ctrl+b, then c       # add a window to a session
1 Ctrl+b, then 1       # switch to window 1 in the same session.  You can change 1 to other numbers.
1 Ctrl+b, then %       # add a pane to window
1 Ctrl+b, then o       # switch panes in the same window
1 Ctrl+b then d        # detach a session
1 tnum attach -t work # re-attach to session 'work'
1 tmux a # re-attach to the last session used
1 Ctrl+d # kill a pane, a window, a session

Details: 

Some bioinformatics software pipeline need long time to run and need us to monitor the progress from time to time. But we often can not keep our computer always on to keep the linux session on. Also the internet connection may get lost, then you will miss the session.

Thanks to the Linux developers for the tmux command, which can easily help us keep as many sessions as we want alive.

Jump start: If you don't want to understand the details, here is my simple and easy way to use tmux.

First, login to O2 using Linux/Mac terminal or Putty on Windows. Next you can start a tmux session: 

1 ld32@login01:~$ tmux new -s work

Please notice, we started the tmux session on login01 here. Your node (computer) may be login02 or any other login nodes. You need remember it so you can come back later.

Now you should have a window like: 

So there are 1 windows (0:bash*) inside this session.  The * means the window is active now.

To add one more window, you can type Ctrl+b (hold the ctrl or control key, then type "b"), then c. You get something like: 

So there are two windows now, 0:bash- and 1:bash*. And 1:bash* is active window now. 

When you need turn off your computer or log out of O2,  You can type Ctrl+b, then d for detach the session.

When you want to go back to the previous session, first log on to O2 as before, check if you land on the original tmux session's starting node or not, if not ssh to the original node (computer), then re-attach:

1 2 ld32@login02:~$ ssh login01 ld32@login01:~$ tnum attach -t work

Or if  you just want to go the most recent active session:

1 ld32@login01:~$ tmux a

If you want additional pane in a window, please type Ctrl + b, then %, then you got: 

Now type Ctrl + b, then o switch to the first pane, run this command: 

1 2 3 4 5 6 7 8 9 ld32@login01:~$ srun --pty -p interactive -t 0-12:0:0 --mem 2G -n 1 /bin/bash # here is the output: srun: job 14996113 queued and waiting for resources srun: job 14996113 has been allocated resources ld32@compute-e-16-231:~$ # Now I am on computer-e-231, so that I can run my software ld32@computer-e-16-231:~$ ./my_fancy_analysis.sh

Now type Ctrl + b, then o switch to the second pane, run this command: 

1 2 3 4 5 6 ld32@login01:~$ ssh computer-e-16-231 ld32@compute-e-16-231:~$ # Now I am on computer-e-231, so that I can check software (my_fancy_analysis.sh) running status from this pane ld32@computer-e-16-231:~$ htop -u $USER

So you can monitor process running status from the seond pane while the software is running in the first pane:

For more information on tmux

Please refer to the tmux README on Github: https://github.com/tmux/tmux/blob/master/README