配置tmux在機器重啓後自動恢復tmux工做現場,告別重啓恐懼症

1. 問題出現的背景

經過ssh遠程登陸服務器在上面工做,有時候頗有可能因爲網絡斷開而致使ssh連接斷開,或者下班後想繼續在家登陸到服務器繼續工做。這些狀況都須要服務器保持咱們的工做環境,好比,vim打開的代碼,正在運行的程序等等。python

爲了保持遠程服務器上的工做現場,咱們能夠選用screen、tmux來知足這個需求。可是一旦遠程服務器因爲斷電、操做系統異常等緣由重啓,原先的screen、tmux會話也就沒有了。其中運行的一些程序也就再也不繼續跑了。git

固然,能夠把須要跑的程序配置成開機自動運行。但程序print出來的一些狀態不能很容易的觀察到(固然能夠把print信息寫入log文件,或者把print信息重定向到文件,這就須要更改程序,也不利於實時觀察測試程序)。而其它工做現場好比正在編輯的某個代碼文件就不存在了。github

總而言之,自動恢復重啓以前的工做現場仍是頗有必要的。shell

2. 配置tmux使其自動保存會話狀態

須要知足的條件:vim

  • 1 tmux >= 1.9
  • 2 tmux插件管理器: TMP
  • 2 插件: tmux-resurrent

3 插件:tmux-continuumwindows

2.1 安裝tmux

sudo apt-get install tmux

 

2.2 安裝tmux插件管理器和插件

該部分安裝參照其官方網址的說明便可。bash

如下是安裝好tmux後的配置文件.tmux.conf共參考:服務器

# set shell
set -g default-shell /bin/bash

# ------ general ------------------------------------
set -g prefix2 C-a
bind C-a send-prefix -2

set -g escape-time 0
# set -g base-index 0
set -g renumber-windows on
# set -g mouse on
# set -wg pane-base-index 1

# rename-window
set -wg allow-rename off
set -wg automatic-rename off

# last-window
bind a last

# retain current path
bind c new-window -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"
bind '"' split-window -c "#{pane_current_path}"

# restart and edit
bind r source ~/.tmux.conf\; display "tmux config sourced"
bind e neww -n tmux-config "\${EDITOR:-vim} ~/.tmux.conf"

# ------ move around --------------------------------
bind -r h select-pane -L 
bind -r l select-pane -R
bind -r j select-pane -D
bind -r k select-pane -U
# ------ vi -----------------------------------------
bind -t vi-copy v begin-selection
bind -t vi-copy y copy-selection

# ------ status theme -------------------------------
set -g message-style "bg=#00346e, fg=#ffffd7"        # tomorrow night blue, base3

set -g status-style "bg=#00346e, fg=#ffffd7"   # tomorrow night blue, base3
set -g status-left "#[bg=#0087ff] ❐ #S "       # blue
set -g status-left-length 400
set -g status-right "#{?client_prefix, ⌨ , } #[bg=#0087ff] #(whoami)@#h #[bg=red] %Y-%m-%d %H:%M "
set -g status-right-length 600

set -wg window-status-format " #I #W "
set -wg window-status-current-format " #I #W "
set -wg window-status-separator ""
set -wg window-status-current-style "bg=red" # red
set -wg window-status-last-style "fg=red"

set -wg pane-active-border-style "fg=blue"
set -wg pane-border-style "fg=#585858"       # base01

# automatic restore
set -g @continuum-restore 'on'

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com/user/plugin'
# set -g @plugin 'git@bitbucket.com/user/plugin'
run '~/.tmux/plugins/tpm/tpm'

 

3. 配置開機恢復保存的tmux會話

3.1 編寫回復tmux的腳本,參考以下例子:

#!/bin/bash
# description "Start Tmux"

# Sleep for 5 seconds. If you are starting more than one tmux session
#   "at the same time", then make sure they all sleep for different periods
#   or you can experience problems
/bin/sleep 5
# Ensure the environment is available
source /home/ebu/.bashrc
# Create a new tmux session named newscrawler..
/usr/bin/tmux new-session -d -s newscrawler
# ...and control the tmux session (initially ensure the environment
#   is available, then run commands)
# /usr/bin/tmux send-keys -t newscrawler:0 "source /home/ebu/.bashrc" C-m
/bin/sleep 3
/usr/bin/tmux send-keys -t newscrawler:0 "python ant_client.py" C-m
/bin/sleep 3
/usr/bin/tmux send-keys -t newscrawler:1 "python ant_client.py" C-m
/bin/sleep 3
/usr/bin/tmux send-keys -t newscrawler:3 "top" C-m

 

以上腳本須要注意的幾點:網絡

  • 1 建立的新tmux會話newscrawler的相關信息已經被tmux-resurrect保存在了~/.tmux/resurrect/last文件裏面
  • 2 在新session的不一樣窗口裏面分別運行程序以前,最好要先sleep幾秒鐘,否則你的程序極可能運行失敗。

3.2 配置開機運行上述腳本

3.2.1 能夠寫入/etc/rc.local文件裏面:

# By default this script does nothing.
/home/veelion/reboot-tmux-ant_client.sh
exit 0

 

3.2.2 也能夠配置到crontab裏面:

# start crawler at reboot
@reboot /home/ebu/reboot-tmux-ant_client.sh

 

 

python學習筆記整理於猿人學網站的python基礎教程session

相關文章
相關標籤/搜索