標籤: centos

  • 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
  • 排程檢查服務是否正常

    好久以前遇到一些伺服器鬼故事,所以參考網路上的資料,弄了一個排程檢查系統服務的機制,配合上 crontab 可以使用。

    原則上,這個做法和把程式 try / catch 包起來一樣,掩耳盜鈴的行徑。在沒有安全疑慮的狀況下,建議配合 log 確認系統紀錄,這樣可以保持穩定又可以找到系統問題,不失為一個權宜之計。

    使用的環境是 CentOS7 / Linode VPS ,從指令上來看應該 debian 系列的也可以用才對。

    操作上只要把 service 的變數替換為對應的服務名稱即可,以下是 mysql 檢查重啟的 code.

    #!/bin/bash
    
    PATH=/usr/sbin:/usr/bin:/sbin:/bin
    
    service=mysql
    if [[ ! "$(/usr/sbin/service $service status)" =~ "running" ]]
    then
        echo "`date "+%Y-%m-%d %H:%M:%S"` | [logadm -on@`date "+%Y%m%d%H%M%S"`] "
        echo "$service is stop!!"
        /usr/sbin/service $service start
    else
        echo "`date "+%Y-%m-%d %H:%M:%S"` | [logadm -on@`date "+%Y%m%d%H%M%S"`] "
        echo "$service is running"
    fi

    同場加映,今天寫這篇時,網路上再找了一下,找到一些比較進階的檢查 code: