Bernardo Bernardo - 11 months ago 125
Linux Question

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

I had a .txt script that was running fine, opening several

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
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:=

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

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

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.

Since I am assuming the problem is with
, 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.

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.

Thank you.

Answer Source

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
tmux send-keys -t real "tmux send-keys -t NAME1 'COMMAND' ENTER" ENTER
tmux send-keys -t real "tmux send-keys -t NAME2 'COMMAND' ENTER" ENTER
tmux send-keys -t real "tmux send-keys -t NAME3 'COMMAND' ENTER" ENTER
tmux send-keys -t real "tmux send-keys -t NAME4 'COMMAND' ENTER" ENTER
tmux send-keys -t real "tmux send-keys -t NAME5 'COMMAND' ENTER" ENTER
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

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