经过ssh远程登陆服务器在上面工做,有时候颇有可能因为网络断开而致使ssh连接断开,或者下班后想继续在家登陆到服务器继续工做。这些状况都须要服务器保持咱们的工做环境,好比,vim打开的代码,正在运行的程序等等。python
为了保持远程服务器上的工做现场,咱们能够选用screen、tmux来知足这个需求。可是一旦远程服务器因为断电、操做系统异常等缘由重启,原先的screen、tmux会话也就没有了。其中运行的一些程序也就再也不继续跑了。git
固然,能够把须要跑的程序配置成开机自动运行。但程序print出来的一些状态不能很容易的观察到(固然能够把print信息写入log文件,或者把print信息重定向到文件,这就须要更改程序,也不利于实时观察测试程序)。而其它工做现场好比正在编辑的某个代码文件就不存在了。github
总而言之,自动恢复重启以前的工做现场仍是颇有必要的。shell
须要知足的条件:vim
3 插件:tmux-continuumwindows
sudo apt-get install 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'
#!/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
以上脚本须要注意的几点:网络
# By default this script does nothing. /home/veelion/reboot-tmux-ant_client.sh exit 0
# start crawler at reboot @reboot /home/ebu/reboot-tmux-ant_client.sh
python学习笔记整理于猿人学网站的python基础教程session