tmux tutorial on ubuntu 16.04

tmux tutorial on ubuntu 16.04

Guide

install

sudo apt-get -y install tmux

usage

1) 默认创建一个会话,以数字命名。(不推荐)
[[email protected] ~]# tmux

2)新建会话,比如新创建一个会话以"ccc"命名
[[email protected] ~]# tmux new -s ccc

3) 加上参数-d,表示在后台新建会话
[email protected]:~# tmux new -s shibo -d
[email protected]:~# tmux ls
shibo: 1 windows (created Tue Oct  2 19:22:32 2018) [135x35]

4)查看创建得所有会话
[[email protected] ~]# tmux ls
0: 1 windows (created Wed Aug 30 17:58:20 2017) [112x22](attached)    #attached表示该会话是当前会话
aaa: 2 windows (created Wed Aug 30 16:54:33 2017) [112x22]
ccc: 1 windows (created Wed Aug 30 17:01:05 2017) [112x22]

5)从终端环境进入会话
[[email protected] ~]# tmux attach -t aaa 

6)快捷键退出会话:
登到某一个会话后,依次按键ctrl-b + d,这样就会退化该会话,但不会关闭会话;
按键ctrl + d,就会在退出会话的通话也关闭了该会话。

7)关闭会话(销毁会话)
[[email protected] ~]# tmux ls
aaa: 2 windows (created Wed Aug 30 16:54:33 2017) [112x22]
bbb: 1 windows (created Wed Aug 30 19:02:09 2017) [112x22]

[[email protected] ~]# tmux kill-session -t bbb

[[email protected] ~]# tmux ls
aaa: 2 windows (created Wed Aug 30 16:54:33 2017) [112x22]

8)重命名会话
[[email protected] ~]# tmux ls  
wangshibo: 1 windows (created Sun Sep 30 10:17:00 2018) [136x29] (attached)

[[email protected] ~]# tmux rename -t wangshibo kevin

[[email protected] ~]# tmux ls
kevin: 1 windows (created Sun Sep 30 10:17:00 2018) [136x29] (attached)

9)发送命令到回话并执行
tmux send -t session_name "command" ENTER

nohup

nohup <command> [argument…] &

使用tmux打开一个终端窗口,可以在窗口里执行一个长时间运行的交互式命令操作,令其一直在后台跑着,并且在按键ctrl-b-d后,可以无感知的退出窗口,
而退出后窗口不会关闭,即窗口里执行的交互命令也不会结束。这比起传统的”nohup commang & (然后按ctrl+c)”的方式要好用很多。

tmux new -s aaa

sudo apt-get -y update 
ctrl+b + d

similar tools

  • screen

Reference

History

  • 20181214: created.
坚持技术分享,您的支持将鼓励我继续创作!