跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • tmux 怎么用 shell 脚本操作?
未分類
19 8 月 2020

tmux 怎么用 shell 脚本操作?

tmux 怎么用 shell 脚本操作?

資深大佬 : wsseo 11

#!/bin/sh #服务器要起一些程序,目前在测试阶段,所有很多组件都在前台运行 #每次都用 tmux 一个一个启动,ctrl+b c,ctrl+b c....,比较麻烦 tmux new -s testapp 执行 app01,有前台输入 新建一个窗口,执行 app02 再建一个窗口,执行 app03 ... 最后退出 这样的操作用 shell 脚本怎么写? 

大佬有話說 (16)

  • 資深大佬 : ysc3839

    tmux send-keys?
    https://unix.stackexchange.com/questions/409861/is-it-possible-to-send-input-to-a-tmux-session-without-connecting-to-it

  • 資深大佬 : nickchenyx

    恰好我也有这样的诉求,我写了个 tmux-init.sh 脚本完成这个工作,等于重启的话执行下脚本就可以拉起工作台了。

    “`shell
    # new bastion session
    tmux new -s bastion -d
    # rename window
    ## — DEV —
    tmux rename-window -t “bastion:1” dev
    # send command
    tmux send -t “bastion:dev” ‘/Users/nickchen/workspace/bastion.sh java-run “cd /home/nickchen/java-app/”‘ Enter
    # split window horizontally
    tmux split-window -t “bastion:dev”
    tmux send -t “bastion:dev” ‘/Users/nickchen/workspace/bastion.sh inner-01 “cd /opt”‘ Enter
    # split window vertically
    tmux split-window -h -t “bastion:dev”
    tmux send -t “bastion:dev” ‘/Users/nickchen/workspace/bastion.sh inner-02 “cd /opt”‘ Enter
    “`

    这是一部分啦,本质上都是一样的原理,去看下 tmux send 的操作就行,我这里是创建多个 window + 每个 window 分多个 panel 。

  • 資深大佬 : lululau

    systemd, supervisor, foreman 之类的不香吗

    非用 tmux 的话,直接调 tmux sub-commands 也可以,或者用 tmuxinator 之类的

  • 資深大佬 : vsyour

    “`bash
    cd ;for i in `ls results_*/ -d`;do cd;cd $i;tmux new -s $i -d;tmux send -t “$i” ‘for i in `seq 3`;do python3 run.py ;sleep 600;done’ Enter;done

    “`
    参考
    功能介召:
    1. 扫描当前目录中 results_ 开头的目录
    2. 进入当前用户,进入 results_ 列表中的一个目录.
    3. 创建一个以 results_xxxx 命名的 tmux 放到后台
    4. 执行 results_xxxx 目录下的 run.py 脚本. 执行 3 次.每次执行完后暂停 10 分钟.

  • 資深大佬 : chenqh

    tmuxp?

  • 資深大佬 : chenqh

    还有输入?那忽略我吧

  • 主 資深大佬 : wsseo

    @chenqh 是有输出,笔误了

  • 資深大佬 : ksc010

    https://github.com/tmux-python/libtmux
    python 操作 tmux 的库

  • 資深大佬 : chenqh

    @wsseo 那就 tmuxp

  • 資深大佬 : chenqh

    tmux 关闭自动 rename

    vim ~/.tmux.conf

    添加
    “`

    set-window-option -g automatic-rename off
    set-option -g allow-rename off
    “`

  • 主 資深大佬 : wsseo

    先用笨方法
    “`
    #!/bin/sh
    #服务器要起一些程序,目前在测试阶段,所有很多组件都在前台运行
    #每次都用 tmux 一个一个启动,ctrl+b c,ctrl+b c….,比较麻烦
    tmux new -s testapp -d
    tmux send-keys -t testapp:0 ‘ping www.baidu.com’ C-m
    tmux new-window -t testapp:1
    tmux send-keys -t testapp:1 ‘vi haha.txt’ C-m
    …

  • 主 資深大佬 : wsseo

    tmux new-window -t testapp:1 -d

  • 資深大佬 : LudwigWS

    挺有意思的,学习一下

  • 資深大佬 : ksc010

    还有一个方法就 安装 mux
    使用编写配置文件

  • 資深大佬 : secondwtq

    我正好搞过这个,需求是给 B 站直播 up 主录回放。方案是在一个 tmux pane 里面起一个循环轮询 B 站 API,检测到状态在直播就再开一个 pane 用 you-get 下载,完后 ffmpeg 转一遍: https://github.com/secondwtq/bmon

    用不着 send-keys,直接东西写进一脚本里面 split-pane/new-window/split-window 就行,不然你开的 shell 还得手动关

  • 資深大佬 : billgreen1

    tmuxinator

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具