配置tmux在机器重启后自动恢复tmux工作现场,告别重启恐惧症
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
sudo apt-get install tmux
2.2 安装tmux插件管理器和插件
该部分安装参照其官方网址的说明即可。
以下是安装好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基础教程
配置tmux在机器重启后自动恢复tmux工作现场,告别重启恐惧症的更多相关文章
- Linux系统下/tmp目录文件重启后自动删除,不重启自动删除10天前的/TMP的文件(转)
/tmp目录文件重启后自动删除现在知道有ubuntu和solaris系统source:http://blog.chinaunix.net/uid-26212859-id-3567875.html经常会 ...
- windows 10 删除库后自动恢复的解决方法
目录 什么是windows 库? 手动删除不行吗? 如何正确的"删除"? title: windows 10 删除库后自动恢复的解决方法 date: 2019-06-09 15:4 ...
- EC2 开启 IPV6 访问 和 禁止重启后自动分配IP地址
EC2 开启 IPV6 访问 和 禁止重启后自动分配IP地址进入 VPC 控制台,对当前 VPC 添加 IPV6 CIDR 块对该 VPC 的路由表进行修改,添加其它路由,第一个空填::/0,第二个空 ...
- Linux--/tmp目录文件重启后自动删除
源博客 http://blog.itpub.net/24996904/viewspace-769327/ 在/etc/default/目录下有个rcS文件,文件内容如下:## /etc/default ...
- Mac突然没有声音但是重启后可以恢复
命令行操作方式 今天又发现了Mac上的一个BUG,有时候在工作之余我们去吃饭的时候Mac经常会进入睡眠状态,但是有的时候从睡眠状态激活后,本来想听个音乐,但是突然发现音乐不可以用了,以前每次都是重 ...
- 配置hooks使svn提交后自动同步客户端代码(客户端与服务端在同一台机器上)
1.配置svn的hooks 2.实例演示 1.配置svn的hooks 1.1)配置情况 承接上篇svn搭建的文章,今次继续使用上篇文章的配置 上篇文章的地址:linux下搭建svn代码库 svn仓库所 ...
- 解决RAID重启后自动更名为md127
创建完raid后查看/etc/有没有生成mdadm.conf文件 如果没有执行命令:mdadm --detail --scan >> /etc/mdadm.conf 编辑配置文件/etc/ ...
- mount --bind 重启后失效的解决办法
vsftp不支持软链接,可以用mount来支持不同的目录结构 mount --bind /home/www/web/ROOT/img/upload /ftp/private/upload 重启后失效. ...
- Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法
原文:Installshield关于.NET安装时需要重启动的处理办法,以及延伸出的重启后继续安装的安装包的一点想法 很多朋友做安装包的时候,所打包的软件需要.NET Framework之类的环境,他 ...
随机推荐
- golang 日志模块(log)
log 日志 log 模块可以自定义log 对象, 也可以使用log默认对象的日志方法 func New 创建log对象 func New(out io.Writer, prefix string, ...
- 设置主机防火墙规则(iptables规则设置及其与firewalld的生死纠葛)
一.什么是firewalld防火墙? firewalld防火墙在Linux主机里其实就是一道隔离工具,它只对进出主机的请求做判断处理.也就是说它只管进出,至于你进来后做了什么,就不在firewalld ...
- jquery获得 当前页面url的变量
(function($){ $.extend({ urlGet:function () { var aQuery = window.location.href.split("?") ...
- python lanbda匿名函数(20)
在python开发中常规的函数在调用之前都需要先声明,而python还有一种匿名函数,有速写函数的功能并且匿名函数不需要声明也没有函数名字,完全不需要担心函数名冲突,具体的妙用还需要从实战练习中多多积 ...
- 【leetcode算法-简单】53. 最大子序和
[题目描述] 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: ...
- 《Tsinghua os mooc》第17~20讲 同步互斥、信号量、管程、死锁
第十七讲 同步互斥 进程并发执行 好处1:共享资源.比如:多个用户使用同一台计算机. 好处2:加速.I/O操作和CPU计算可以重叠(并行). 好处3:模块化. 将大程序分解成小程序.以编译为例,gcc ...
- WCF-简单 配置文件
一.服务端配置文件 主要包括 1.services 配置服务节点 <!--name 指的是契约实现类--> <service name="WcfLib.User2" ...
- Android虚拟机安装apk
将apk放入platform-tools 打开虚拟机之后 进入该目录执行命令
- Java基础---Java 开发工具IntelliJ IDEA 快捷操作
IDE中快捷编写函数的方法 public static void main(String[] args) ===> psvm System.out.println(); ====> sou ...
- c++ string类型成员变量在调用构造函数后未能正确赋值
struct RelItem{ string segName; Elf32_Rel* rel; string relName; RelItem(string seg, int addr, string ...