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

半世苍凉 提交于 2019-12-12 04:24:02

问题


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

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 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.

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.


回答1:


I'm not 100% sure what you're looking for, but here's an answer to my best guess. The following script will create a new tmux session with three windows:

  • shell session starting in the default directory (your home directory, if unset)
  • shell session starting in /tmp
  • vi on the file .bashrc in your home directory

Put the following in a file, save it, and run it:

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

You can then attach to the session using the following command:

$ tmux attach -t foo

There are many, many things you can automate, and many nuances to some of the commands (c.f. tmux new-window 'vi ~/.bashrc' vs. tmux new-window vi ~/.bashrc).




回答2:


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

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



来源:https://stackoverflow.com/questions/39765692/how-to-use-a-bash-script-to-run-multiple-non-ending-commands-on-linux-using-tmux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!