如何使用bash脚本在使用Tmux的Linux上运行多个非结束命令 [英] How to use a bash script to run multiple non-ending commands on Linux using Tmux
问题描述
我有一个运行良好的.txt脚本,它打开了几个screen
窗口并运行了几个命令.然后,我添加了新行,保存了该行,运行了脚本,该行不再起作用.我决定将其恢复原状,但仍然无法正常工作,并且代码与以前完全相同.我听说screen
是非常多虫的,不再受支持,所以我想是因为这个原因.这是代码:
I had a .txt script that was running fine, opening several screen
windows and running several commands. Then, I added a new line, saved it, ran the script and it no longer works. I decided to put it back to how it was, but it still does not work, and the code is exactly the same as before. I have heard that screen
is very buggy and no longer supported, so I guess it is because of that. This is the code:
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
,其中屏幕实线是.txt文件的名称.如果您没有注意到,我正在与ros合作.因此该命令在不同的窗口中运行了不同的节点.
The way to run it was running the command screen -S real -c screen-real
, where screen-real was the name of the .txt file. I am working with ros, if you haven't noticed. So the command ran different nodes in different windows.
由于我假设问题出在screen
,所以我决定尝试使用Tmux.尽管如此,如果您看到一种修复先前脚本的方法,那么我可以接受.
Since I am assuming the problem is with screen
, I have decided to try and use Tmux instead. Nonetheless, if you see a way for fixing the previous script, I am open to it.
所以,我的问题是:我该如何为Tmux编写bash脚本,使其打开5个窗口并在每个窗口上运行5组不同的命令?所有命令都是无止境的,即它们没有完成运行.这就是为什么我需要打开几个窗口的原因.
So, my problem is: how should I write my bash script for Tmux in a way that it opens 5 windows and runs 5 different set of commands on each one of them? All of the commands are non-ending, i.e., they do not finish running. This is why I need several windows open.
我已经在互联网的每个角落搜索了一个答案,但是通常需要运行的命令才能完成,对于我来说,我在其他答案中看到的代码都不适用.
I have searched every corner of the internet for an answer, but the commands that are usually necessary to run finish, and in my case the code that I have seen in other answers does not apply.
如果您需要更多信息,请提出要求.我希望我足够清楚.
If you need more information, please ask for it. I hope I was clear enough.
推荐答案
我已经找到了答案. 这是我为使用5个窗口启动Tmux会话而编写的脚本,每个窗口运行几个独立的命令:
I already found an answer. This is the script that I wrote for starting a Tmux session with 5 windows, each running several independent commands:
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个窗口,因为第一个窗口仅用于向其他窗口发送命令.最后,我选择了窗口5,以便在连接时显示窗口5
So this code creates actually 6 windows because the first one is used to just send commands to the other ones. In the end I select window 5 so that when I attach it shows window 5
这篇关于如何使用bash脚本在使用Tmux的Linux上运行多个非结束命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!