如何使用 bash 脚本在 Linux 上使用 Tmux 运行 多个非结束命令

How to use a bash script to run multiple non-ending commands on Linux using Tmux

我有一个 .txt 脚本 运行ning 很好,打开几个 screen windows 和 运行ning 几个命令。然后,我添加了一个新行,保存它,运行 脚本,它不再有效。我决定把它放回原来的样子,但还是不行,代码和以前一模一样。我听说 screen 有很多错误并且不再受支持,所以我想是因为这个。这是代码:

screen -t ur10_server 0
stuff "cd ~/catkin_ws; source devel/setup.bash;
"
stuff "roslaunch ur_bringup ur10_bringup_joint_limited.launch robot_ip:=192.168.1.102
"

screen -t moveit 1
stuff "sleep 3; cd ~/catkin_ws; source devel/setup.bash;
"
stuff "roslaunch ur10_moveit_config ur10_moveit_planning_execution.launch limited:=true
"

screen -t enhance_ur10 2
stuff "sleep 10; cd ~/catkin_ws; source devel/setup.bash;
"
stuff "roslaunch ur10_3d_calibration ur10_nodes.launch
"

screen -t rviz 3
stuff "sleep 10; cd ~/catkin_ws; source devel/setup.bash;
"
stuff "roslaunch ur10_moveit_config moveit_rviz.launch config:=true;
"

screen -t calibration_manager 4
stuff "sleep 10; cd ~/catkin_ws; source devel/setup.bash;
"
stuff "rosrun ur10_3d_calibration interaction_manager.py
"

screen -t editor 5
stuff "sleep 10; cd ~/catkin_ws; source devel/setup.bash; roscd ur10_3d_calibration
"

hardstatus alwayslastline
hardstatus string "%{= KW} %H [%`] %{= Kw}|%{-} %-Lw%{=bW}%n%f %t%{-}%+Lw %=%C%a%Y-%M-%d"

select 0
bind "." next
bind "," next

运行 的方法是 运行 命令 screen -S real -c screen-real,其中 screen-real 是 .txt 文件的名称。如果您没有注意到,我正在使用 ros。所以命令运行不同的节点在不同的windows.

因为我假设问题出在 screen,所以我决定尝试使用 Tmux。尽管如此,如果您看到修复先前脚本的方法,我愿意接受。

所以,我的问题是:我应该如何为 Tmux 编写我的 bash 脚本,以便在每个脚本上打开 5 windows 和 运行s 5 个不同的命令集其中之一?所有命令都是非结束的,即它们不会完成 运行ning。这就是为什么我需要几个 windows open.

我已经在互联网的每个角落搜索了答案,但是 运行 完成通常需要的命令,在我的情况下,我在其他答案中看到的代码不适用。

如果您需要更多信息,请询问。我希望我说得够清楚了。

我不是 100% 确定您在寻找什么,但这是我的最佳猜测的答案。以下脚本将使用三个 windows:

创建一个新的 tmux 会话
  • shell 会话从默认目录(您的主目录,如果未设置)开始
  • shell 会话开始于 /tmp
  • vi 在您的主目录中的文件 .bashrc 上

将以下内容放入文件中,保存并运行它:

tmux new-session -s foo -d
tmux new-window -c /tmp
tmux new-window 'vi ~/.bashrc'

然后您可以使用以下命令附加到会话:

$ tmux attach -t foo

有很多很多事情可以自动化,一些命令有很多细微差别(c.f。tmux new-window 'vi ~/.bashrc'tmux new-window vi ~/.bashrc)。

我已经找到答案了。 这是我为启动一个 Tmux 会话而编写的脚本,其中有 5 个 windows,每个 运行 个独立的命令:

tmux new-session -d -s real

## Create the windows on which each node or .launch file is going to run
tmux send-keys -t real 'tmux new-window -n NAME1 ' ENTER
tmux send-keys -t real 'tmux new-window -n NAME2 ' ENTER
tmux send-keys -t real 'tmux new-window -n NAME3 ' ENTER
tmux send-keys -t real 'tmux new-window -n NAME4 ' ENTER
tmux send-keys -t real 'tmux new-window -n NAME5 ' ENTER
tmux send-keys -t real 'tmux new-window -n NAME6 ' ENTER

## Send the command to each window from window 0
# NAME1
tmux send-keys -t real "tmux send-keys -t NAME1 'COMMAND' ENTER" ENTER
# NAME2
tmux send-keys -t real "tmux send-keys -t NAME2 'COMMAND' ENTER" ENTER
# NAME3
tmux send-keys -t real "tmux send-keys -t NAME3 'COMMAND' ENTER" ENTER
# NAME4
tmux send-keys -t real "tmux send-keys -t NAME4 'COMMAND' ENTER" ENTER
# NAME5
tmux send-keys -t real "tmux send-keys -t NAME5 'COMMAND' ENTER" ENTER
# NAME6
tmux send-keys -t real "tmux send-keys -t NAME6 'COMMAND' ENTER" ENTER

## Start a new line on window 0
tmux send-keys -t real ENTER

## Attach to session
tmux send-keys -t real "tmux select-window -t NAME5" ENTER
tmux attach -t real

所以这段代码实际上创建了 6 个 windows 因为第一个只是用来向其他的发送命令。最后我 select window 5 这样当我附加它时显示 window 5