bash 启动一个新的 tmux 会话并分离它,所有这些都在一个 shell 脚本中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33426159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:50:48  来源:igfitidea点击:

Starting a new tmux session and detaching it, all inside a shell script

bashshellunixvagranttmux

提问by Swarup Donepudi

I am trying to create a new tmux session and execute the command 'vagrant up'. 'Vagrant up' takes more than 3 hours so I want to detach the session so that I can come back later and check the status of that command by attaching back to the same session.

我正在尝试创建一个新的 tmux 会话并执行命令“vagrant up”。“Vagrant up”需要 3 个多小时,因此我想分离会话,以便稍后返回并通过附加到同一个会话来检查该命令的状态。

I followed the answer specified in the StackOverflow postto accomplish the same.

我按照StackOverflow 帖子中指定的答案来完成相同的操作。

I am getting the error no session found. Here is my code:

我收到错误no session found。这是我的代码:

    $cat tmux_sh.sh
    #!/bin/bash
    echo "step 1"
    tmux new-session -d -s rtb123 'vagrant up'
    echo "step 2"
    tmux detach -s rtb123

    $./tmux_sh.sh
    step 1
    step 2
    session not found: rtb123

回答by mlv

Start a shell, and send vagrant up to it, so you can see the errors.

启动一个 shell,然后发送 vagrant 到它,这样你就可以看到错误了。

tmux new-session -d -s rbt123
tmux send-keys 'vagrant up' C-m
tmux detach -s rtb123

The C-mmeans hit return.

C-m手段回车。

回答by petersohn

You are using the -dswitch when creating the session. This means that the session will start detached, so you don't need to use the detach command. Besides, if your session is not running when you try to detach, it means that it no longer exists, so your command probably exited.

-d创建会话时您正在使用开关。这意味着会话将开始分离,因此您不需要使用 detach 命令。此外,如果您尝试分离时会话未运行,则意味着它不再存在,因此您的命令可能已退出。