1. 问题出现的背景

通过ssh远程登录服务器在上面工作,有时候很有可能由于网络断开而导致ssh链接断开,或者下班后想继续在家登录到服务器继续工作。这些情况都需要服务器保持我们的工作环境,比如,vim打开的代码,正在运行的程序等等。

为了保持远程服务器上的工作现场,我们可以选用screen、tmux来满足这个需求。但是一旦远程服务器由于断电、操作系统异常等原因重启,原先的screen、tmux会话也就没有了。其中运行的一些程序也就不再继续跑了。

当然,可以把需要跑的程序配置成开机自动运行。但程序print出来的一些状态不能很容易的观察到(当然可以把print信息写入log文件,或者把print信息重定向到文件,这就需要更改程序,也不利于实时观察测试程序)。而其它工作现场比如正在编辑的某个代码文件就不存在了。

总而言之,自动恢复重启之前的工作现场还是很有必要的。

2. 配置tmux使其自动保存会话状态

需要满足的条件:

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

3 插件:tmux-continuum

2.1 安装tmux

  1. sudo apt-get install tmux

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

该部分安装参照其官方网址的说明即可。

以下是安装好tmux后的配置文件.tmux.conf共参考:

  1. # set shell
  2. set -g default-shell /bin/bash
  3.  
  4. # ------ general ------------------------------------
  5. set -g prefix2 C-a
  6. bind C-a send-prefix -2
  7.  
  8. set -g escape-time 0
  9. # set -g base-index 0
  10. set -g renumber-windows on
  11. # set -g mouse on
  12. # set -wg pane-base-index 1
  13.  
  14. # rename-window
  15. set -wg allow-rename off
  16. set -wg automatic-rename off
  17.  
  18. # last-window
  19. bind a last
  20.  
  21. # retain current path
  22. bind c new-window -c "#{pane_current_path}"
  23. bind % split-window -h -c "#{pane_current_path}"
  24. bind '"' split-window -c "#{pane_current_path}"
  25.  
  26. # restart and edit
  27. bind r source ~/.tmux.conf\; display "tmux config sourced"
  28. bind e neww -n tmux-config "\${EDITOR:-vim} ~/.tmux.conf"
  29.  
  30. # ------ move around --------------------------------
  31. bind -r h select-pane -L
  32. bind -r l select-pane -R
  33. bind -r j select-pane -D
  34. bind -r k select-pane -U
  35. # ------ vi -----------------------------------------
  36. bind -t vi-copy v begin-selection
  37. bind -t vi-copy y copy-selection
  38.  
  39. # ------ status theme -------------------------------
  40. set -g message-style "bg=#00346e, fg=#ffffd7" # tomorrow night blue, base3
  41.  
  42. set -g status-style "bg=#00346e, fg=#ffffd7" # tomorrow night blue, base3
  43. set -g status-left "#[bg=#0087ff] ❐ #S " # blue
  44. set -g status-left-length 400
  45. set -g status-right "#{?client_prefix, ⌨ , } #[bg=#0087ff] #(whoami)@#h #[bg=red] %Y-%m-%d %H:%M "
  46. set -g status-right-length 600
  47.  
  48. set -wg window-status-format " #I #W "
  49. set -wg window-status-current-format " #I #W "
  50. set -wg window-status-separator ""
  51. set -wg window-status-current-style "bg=red" # red
  52. set -wg window-status-last-style "fg=red"
  53.  
  54. set -wg pane-active-border-style "fg=blue"
  55. set -wg pane-border-style "fg=#585858" # base01
  56.  
  57. # automatic restore
  58. set -g @continuum-restore 'on'
  59.  
  60. # List of plugins
  61. set -g @plugin 'tmux-plugins/tpm'
  62. set -g @plugin 'tmux-plugins/tmux-sensible'
  63. set -g @plugin 'tmux-plugins/tmux-resurrect'
  64. set -g @plugin 'tmux-plugins/tmux-continuum'
  65.  
  66. # Other examples:
  67. # set -g @plugin 'github_username/plugin_name'
  68. # set -g @plugin 'git@github.com/user/plugin'
  69. # set -g @plugin 'git@bitbucket.com/user/plugin'
  70. run '~/.tmux/plugins/tpm/tpm'

3. 配置开机恢复保存的tmux会话

3.1 编写回复tmux的脚本,参考如下例子:

  1. #!/bin/bash
  2. # description "Start Tmux"
  3.  
  4. # Sleep for 5 seconds. If you are starting more than one tmux session
  5. # "at the same time", then make sure they all sleep for different periods
  6. # or you can experience problems
  7. /bin/sleep 5
  8. # Ensure the environment is available
  9. source /home/ebu/.bashrc
  10. # Create a new tmux session named newscrawler..
  11. /usr/bin/tmux new-session -d -s newscrawler
  12. # ...and control the tmux session (initially ensure the environment
  13. # is available, then run commands)
  14. # /usr/bin/tmux send-keys -t newscrawler:0 "source /home/ebu/.bashrc" C-m
  15. /bin/sleep 3
  16. /usr/bin/tmux send-keys -t newscrawler:0 "python ant_client.py" C-m
  17. /bin/sleep 3
  18. /usr/bin/tmux send-keys -t newscrawler:1 "python ant_client.py" C-m
  19. /bin/sleep 3
  20. /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文件里面:

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

3.2.2 也可以配置到crontab里面:

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

python学习笔记整理于猿人学网站的python基础教程

配置tmux在机器重启后自动恢复tmux工作现场,告别重启恐惧症的更多相关文章

  1. Linux系统下/tmp目录文件重启后自动删除,不重启自动删除10天前的/TMP的文件(转)

    /tmp目录文件重启后自动删除现在知道有ubuntu和solaris系统source:http://blog.chinaunix.net/uid-26212859-id-3567875.html经常会 ...

  2. windows 10 删除库后自动恢复的解决方法

    目录 什么是windows 库? 手动删除不行吗? 如何正确的"删除"? title: windows 10 删除库后自动恢复的解决方法 date: 2019-06-09 15:4 ...

  3. EC2 开启 IPV6 访问 和 禁止重启后自动分配IP地址

    EC2 开启 IPV6 访问 和 禁止重启后自动分配IP地址进入 VPC 控制台,对当前 VPC 添加 IPV6 CIDR 块对该 VPC 的路由表进行修改,添加其它路由,第一个空填::/0,第二个空 ...

  4. Linux--/tmp目录文件重启后自动删除

    源博客 http://blog.itpub.net/24996904/viewspace-769327/ 在/etc/default/目录下有个rcS文件,文件内容如下:## /etc/default ...

  5. Mac突然没有声音但是重启后可以恢复

    命令行操作方式   今天又发现了Mac上的一个BUG,有时候在工作之余我们去吃饭的时候Mac经常会进入睡眠状态,但是有的时候从睡眠状态激活后,本来想听个音乐,但是突然发现音乐不可以用了,以前每次都是重 ...

  6. 配置hooks使svn提交后自动同步客户端代码(客户端与服务端在同一台机器上)

    1.配置svn的hooks 2.实例演示 1.配置svn的hooks 1.1)配置情况 承接上篇svn搭建的文章,今次继续使用上篇文章的配置 上篇文章的地址:linux下搭建svn代码库 svn仓库所 ...

  7. 解决RAID重启后自动更名为md127

    创建完raid后查看/etc/有没有生成mdadm.conf文件 如果没有执行命令:mdadm --detail --scan >> /etc/mdadm.conf 编辑配置文件/etc/ ...

  8. mount --bind 重启后失效的解决办法

    vsftp不支持软链接,可以用mount来支持不同的目录结构 mount --bind /home/www/web/ROOT/img/upload /ftp/private/upload 重启后失效. ...

  9. Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法

    原文:Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法 很多朋友做安装包的时候,所打包的软件需要.NET Framework之类的环境,他 ...

随机推荐

  1. 使用Apache,压力测试redisson的一般高并发

    安装 Linux linux直接yum -y install httpd-tools,然后ab -V测试 Windows 1查看80端口有没有被占用,netstat -ano | findstr &q ...

  2. Edit Delete Mysql的主从复制

    参考博客 https://www.cnblogs.com/zhoujie/p/mysql1.html Mysql的主从复制至少是需要两个Mysql的服务,当然Mysql的服务是可以分布在不同的服务器上 ...

  3. OS选择题练习

    一.死锁 1.设系统中有n个进程并发,共同竞争资源X,且每个进程都需要m个X资源,为使该系统不会发生死锁,资源X的数量至少为() A.n*m+1 B.n*m+n C.n*m+1-n   D.无法预计 ...

  4. SpringBoot(三)手写starter pom自动配置

    思想:主要是EnableAutoConfiguration在启动的时候会扫描spring.factories并加载 1在resource下面新建META-INF/spring.factories 2在 ...

  5. Win7 JavaEE 安装

    新建四个目录 D:\ApacheServer\eclipse 存放eclipse D:\ApacheServer\jdk jdk安装目录 D:\ApacheServer\apache-tomcat 存 ...

  6. shell习题第27题:带选项的增删用户脚本

    [题目要求] 写一个支持选项的增加或删除用户的shell脚本 #!/bin/bash ]; then echo "Wrong, use bash $0 --add username, or ...

  7. Ubuntu Server 18.04 无法修改 hostname

    对于运维而言,我们希望每台服务器的 hostname 都能体现出它自己的功能/ip,方便排查. ubuntu server live 18.04 的安装流程非常友好,从 ip 到 hostname 都 ...

  8. Java坑人面试题之自动装箱和拆箱后的引用数据类型和基本数据类型的计算

    在Java1.5以后的版本中,引用类型Integer和基本数据类型int之间是可以通过自动装箱和拆箱进行计算的 例如: Integer in = 1; //it means  Integer in = ...

  9. adb 安装 app/apk链接不上设备和安装出现failed_install_user_restricted的解决方法

    1.手机链接电脑,保持网段一致,通过ping 看是否可以ping通 2.如果可以ping通,查看telnet ip 5555 看是否可以连接 3.如果无法连接查看手机是否开启开发者模式中的debug模 ...

  10. aliplay获取播放时长

    <div id="player-con" class="frequency-pic"></div> <link rel=" ...