Linux - Keep SSH sessions with tmux or screen

  • 使用tmux
$ tmux
# now in tmux session 0
$ echo 0
# press Ctrl+b then d to detach
 
$ tmux
# now in tmux session 1
$ echo 1
# press Ctrl+b then d to detach
 
$ tmux list-sessions
0: 1 windows (created Wed Oct 10 15:28:18 2018) [80x23]
1: 1 windows (created Wed Oct 10 15:28:30 2018) [189x49]
 
# attach to session 0
$ tmux attach -t 0
 
# press Ctrl+b then % to create a new left-right pane (see below)
# press Ctrl+b then " to create a new top-bottom pane
# press Ctrl+b then Left/Right or Up/Down to switch between panes
+-------------------------------------------------+
│yuwu@ubuntu:~$          │yuwu@ubuntu:~$          │
│                        │                        │
│                        │                        │
│                        │                        │
│                        │                        │
│                        │                        │
│                        │                        │
│                        │                        │
+-------------------------------------------------+
[1] 0:bash*              "ubuntu" 17:16 10-Oct-18
  • 使用screen
$ screen
# now in screen session 0
$ echo 0
# press Ctrl+a then d to detach
[detached from 22701.pts-0.ubuntu1080]
 
$ screen
# now in screen session 1
$ echo 1
# press Ctrl+a then d to detach
[detached from 22713.pts-0.ubuntu1080]
 
$ screen -r
There are several suitable screens on:
        22713.pts-0.ubuntu1080  (10/10/2018 04:04:19 PM)        (Detached)
        22701.pts-0.ubuntu1080  (10/10/2018 04:03:03 PM)        (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
 
# restore a session
$ screen -r 22713
 
# end the session
$ exit