I'd like to write a script for opening multiple programs (servers) simultanously in separate terminals – doesn't matter which one – and assign different commands to different terminals with commands "landing" inside the correct terminal. Is this possible?
Maybe, something like this:
- open terminal1
- open terminal2 //simultanously with 1.
- command1 //execute in terminal1 without opening a new terminal window
- command2 //execute in terminal2 without opening a new terminal window
- …
Can I somehow label terminal windows so commands are executed inside correct terminal?
I'd also like to watch all terminals while their programs are running – my programs have an argument for printing trace/debug to terminal. So I'd like to see what messages are exchanged between them.
NOTE: I'm less concerned about security of exchanged data since this script should serve as a "simulation". I've configured each server to run from an allocated port on localhost.
Best Answer
Since you mention you solved the problem for your specific situation, below a solution for general purpose. Thanks to
xdotool
's--sync
option, it works pretty reliable in the tests I ran; I could "send" commands to specific terminal windows and it ran perfectly without an exception.How it works in practice
The solution exists from a script, which can be run with two options
-set
and-run
:To set up (open) an arbitrary number of terminal windows, in this example 3:
Three new terminals will open up, their window id is remembered in a hidden file:
For clarity reasons I minimized the terminal window I ran the command from :)
Now that I created three windows, I can send commands to either one of them with the run command (e.g.):
As shown below, the command ran in the second terminal:
Subsequently, I can send a command to the first terminal:
making
sudo apt-get update
run in terminal 1:and so on...
How to set up
The script needs both
wmctrl
andxdotool
:Copy the script below into an empty file, safe it as
target_term
(no extension!) in~/bin
(create the directory~/bin
if necessary.Make the script executable (don't forget) and either log out/in or run:
Now setup your terminal windows, with the number of required windows as an argument:
Now you can "send" commands to either one of your terminals with the command:
The script
Notes
The script is set for
gnome-terminal
, but can be used for any terminal (or other program as well) by changing theapplication
in the head section of the script:No need to say that the script only works with the terminal setup (windows) that was called by the command:
The terminal windows will then be "labelled" by the script, like you mention in your question.
target_term
session, the hidden file, created by the script will simply be overwritten, so there is no need to remove it otherwise.