我需要并行启动多个命令,每个命令在同一会话中各自的 tmux 窗口中,确保每个命令只有一个实例在运行。
理想情况下,窗口应遵循固定的命名和顺序,以便于识别它们。
我认为过去我曾经做过这样的事情:
tmux new-window -t cmd1 -n cmd1 { my command }
然后 tmux 将在最后一个会话中创建一个窗口,命名cmd1
并位于第一个位置(后面是 cmd2 等),或者如果窗口已经存在,则不执行任何操作。
答案1
如果我使用实际整数作为索引,它对我有用:
$ tmux new-window -t 1 -n cmd1 -d sleep 60
$ tmux new-window -t 1 -n cmd1 -d sleep 60
create window failed: index in use: 1
对于非整数索引,它将不起作用:
$ tmux new-window -t cmd1 -n cmd1 -d sleep 60
can't find window i1
手册页引用(tmux 2.5-4):
new-window [-adkP] [-c start-directory] [-F format] [-n window-name] [-t target-window] [shell-command] (alias: neww) Create a new window. With -a, the new window is inserted at the next index up from the specified target-window, moving windows up if necessary, otherwise target-window is the new window location. If -d is given, the session does not make the new window the current window. target-window represents the window to be created; if the target already exists an error is shown, unless the -k flag is used, in which case it is destroyed.