Linux SSH 斷線後保持 session 的工具 – Screen

紀錄一下之前用很久的一個 linux 套件,主要用途是作爲讓 Server 在再斷線後,依然可以跑池長時間非背景運作。

安裝方法,權限問題請自行加上 sudo:

# ubuntu \ debian
apt-get install screen

# centos
yum install screen

一些基本的操作:

# 建立一個新的 screen 
screen -U -S SCREEN_NAME

# 進入已開啟的 screen
screen -drU SCREEN_NAME

# 列出所有 screen
screen -ls

至於進入 screen 內的操作,請參考:

大概可以記住:所有指令都是 ctrl + a 開始。

  • c 是關閉一個建立一個新視窗。另外要離開當前視窗,就和一般 ssh 一樣輸入 exit 就行了,會殺掉當前視窗的 session。
  • d 是離開當前的 screen (keep session)
  • A 大寫的 a 可以重新命名當前的視窗,這很好用

以前同事有分享一個 screenrc 設定檔案,不錯用。請自行取代 /etc/screenrc 檔案:

startup_message off
defencoding utf8
encoding utf8 utf8
#caption always "%{= wk} %{= KY} [%n]%t @ %H %{-} %= %{= KR} %l %{-} | %{= KG} %Y-%m-%d %{-} "
#hardstatus alwayslastline " %-Lw%{= Bw}%n%f %t%{-}%+Lw %=| %0c:%s "
#bindkey ^[z prev
#bindkey ^[x next
termcapinfo xterm*|rxvt* 'ti@:te@'
termcapinfo xterm ti@:te@
vbell off
# C + left : prev
# C + right : next
bindkey "^[[1;5C" next
bindkey "^[O5C" next
bindkey "^[[C" next
bindkey "^[[1;5D" prev
bindkey "^[O5D" prev
bindkey "^[[D" prev


# C-a b : encoding big5
# C-a u : encoding utf8
bind b encoding big5 utf8
bind u encoding utf8 utf8


# C-b $num : move current window to number $num
bind -c move 0 number 0
bind -c move 1 number 1
bind -c move 2 number 2
bind -c move 3 number 3
bind -c move 4 number 4
bind -c move 5 number 5
bind -c move 6 number 6
bind -c move 7 number 7
bind -c move 8 number 8
bind -c move 9 number 9
bindkey "^b" command -c move


# F12 : fast kill
#bindkey "^[[24~" kill


#termcapinfo xterm 'hs:ts=\E]0;:fs=\007:ds=\E]0;bash\007'
#caption always "%{= bk} %{= wk} %-Lw%{by}%n+%f %t%{wk}%{wk}%+Lw %=%{kw}%{= R}%{-}"


shelltitle '$ |csh'


defhstatus "\005t"
#hardstatus on
#caption always "%{= wk} %{= KY} [%n]%t @ %H %{-} %= %{= KR} %l %{-} | %{= KG} %Y-%m-%d %{-} "
#hardstatus alwayslastline " %-Lw%{= Bw}%n%f %t%{-}%+Lw %=| %0c:%s "


#buttom status bar
caption always "%{= .G} %-w%<%{=ub .y}[%n %t]%{= .G}%+w "
hardstatus alwaysignore
#hardstatus alwayslastline "%{= .K} [%l]%<%=%{= .W}@%H %=%{= .y} %Y/%m/%d%{= .m} %C %A"


#shelltitle '$|csh'
#caption always "%{bw}%M/%d %c %{wb} %-w%{c}%n %t%{w}%+w%{k}"


shell -$SHELL

已發佈

分類:

作者: