bash 打开新的 tmux 会话后如何执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19711004/
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 08:27:36  来源:igfitidea点击:

How to execute command after opening new tmux session

bashtmux

提问by doremi

I'm having some trouble getting a new session to execute a command after creation.

我在创建新会话以执行命令时遇到了一些麻烦。

Here's a portion of my .tmux.conf:

这是我的.tmux.conf的一部分:

set-window-option -g automatic-rename off
set-option -g allow-rename off
new -A -s 'main' -n 'servers' 'ls' # troubled line
splitw -h -p 35 htop
splitw -v
splitw -v -t 1
splitw -v -t 1
neww -n 'irc' weechat-curses
selectw -t 0

This is the line that I'm working on:

这是我正在处理的线路:

new -A -s 'main' -n 'servers' 'ls'

Here's how I open tmux:

这是我打开 tmux 的方法:

alias tux='TERM=screen-256color-bce tmux -f ~/.tmux.conf attach-session -t main'

The 'ls' must be causing an error because when it is present, the initial pane doesn't get created. If I change it to 'top', it works fine and the command is executed.

'ls' 肯定会导致错误,因为当它存在时,不会创建初始窗格。如果我将其更改为“top”,则它可以正常工作并执行命令。

So why does top work and not ls (or any other command I try)?

那么为什么 top 起作用而不是 ls (或我尝试的任何其他命令)?

回答by chepner

topruns until you quit. lsexits after it prints the contents of the current directory. This causes the window in which lsruns to close.

top运行直到您退出。ls打印当前目录的内容后退出。这会导致ls运行的窗口关闭。

setw -t servers remain-on-exit on

should keep the the window named 'servers' from closing after its command exits, but it is complicated by the fact that the window does not exist before the new-sessioncommand is run, and after new-sessionreturns, it may be too late to run the setwcommand (although you can try).

应该防止名为“servers”的窗口在其命令退出后关闭,但由于在new-session命令运行之前该窗口不存在这一事实很复杂,并且new-session返回后,运行该setw命令可能为时已晚(尽管你可以试试)。

Instead, create a new session in which the default is for a window to remain after its command exists:

相反,创建一个新会话,其中默认是在命令存在后保留一个窗口:

new -A -s 'main' -n 'servers' 'ls' # troubled line
set -t main set-remain-on-exit on
neww -n 'servers' ls


Based on your last comment, ignore the above, and replace your newcommand with

根据您的最后一条评论,忽略以上内容,并将您的new命令替换为

new -A -s 'main' -n 'servers'
send-keys -t servers.0 ls Enter

This creates a regular window, whose command is a regular shell, but then simulates typing the lscommand at the first shell prompt to provide you with the list of files in that directory. After lscompletes, you are back in the shell, and the pane will continue to exist until the shell itself completes.

这将创建一个常规窗口,其命令是一个常规 shell,但随后模拟ls在第一个 shell 提示符下键入命令以向您提供该目录中的文件列表。后ls完成,你是回到了壳,而面板将继续存在,直到完成自己的壳。