Reputation: 1
I am trying to write a script that will launch multiple new terminals. I have the command to launch the new terminals working fine. Like I said it is supposed to launch multiple new terminals and to do this I have a for loop set up. My problem is that when it launches the first new terminal that new terminal becomes the "active" terminal and it does not come back to the original script to finish the loop and launch the other terminals. Is there a way to release the new terminal and finish working through the loop to launch the other terminals? Thanks for any help.
I have tried sending the process to the background with the & command but for some reason that launches duplicates of the first terminal to be launched and then stops like before. I am new to scripting and I have done a lot of looking and nothing I find seems to apply to what I am trying to do.
for line in ${array_of_terminals_to_launch[@]}
do
grep -F -i $array_of_terminals_to_launch /path/to/file/of/gnome-terminal_commands | sh
this simulates the command in the file that is executed
gnome-terminal -q --geometry="105x8" --window --title"Title 1" --command="command_to_run_in_new_terminal" --tab --title"Tab 2" --command="command_to_run_in_second_tab"
Upvotes: 0
Views: 450
Reputation: 1
So I finally figured it out. My for loop was messed up. The way I had it my grep command was looking for the whole array instead of what was stored in the variable i in the loop.
Upvotes: 0
Reputation: 30621
My problem is that when it launches the first new terminal that new terminal becomes the "active" terminal and it does not come back to the original script to finish the loop and launch the other terminals. Is there a way to release the new terminal and finish working through the loop to launch the other terminals?
Sure, just background the process in your loop by adding a &
after the command:
gnome-terminal -q \
--geometry="105x8" \
--window \
--title "Title 1" \
--command="command_to_run_in_new_terminal" \
--tab \
--title "Tab 2" \
--command="command_to_run_in_second_tab" &
(Note that on Unity in Ubuntu 23.04, this isn't necessary, as the gnome-terminal
command launches a new terminal window via a gnome-terminal-server
daemon process and exits.)
Becoming the "active terminal" is actually just an illusion created by your window manager. The terminal window pops over the window you have selected. There's probably a setting in your window manager to NOT focus new windows.
Therefore, fixing this user experience involves either a) configuring your window manager to not let new window steal focus, or b) start gnome-terminal
minimized.
Any solution I found to the problem of "Start gnome-terminal
minimized" seems to depend on a given window manager having support for this. The solution for X11 is convoluted enough, involving xdotool
. No Wayland solution presents itself.
for
loopOkay, let's go back to the original question where your script wasn't starting more than one process.
It sounds like gnome-terminal
is hanging open after the command finishes. That's not good. If you use the syntax gnome-terminal -- sh -c 'MYCOMMAND'
it will close the terminal after the command finishes.
For example:
COMMANDS=(
'ls; sleep 5'
'ls; sleep 5'
'ls; sleep 5'
'ls; sleep 5'
'echo "hello world"; sleep 5'
'ls; sleep 5'
'echo "hello world"; sleep 5'
)
for cmd in "${COMMANDS[@]}"; do
gnome-terminal -q --geometry="105x8" -- sh -c "$cmd" &
done
But that's no good, it launches everything at once! What if there are hundreds of jobs, that'll max out your CPU and more likely your RAM.
for
loop using GNU ParallelFor your main loop, you can leverage GNU Parallel to spawn multiple terminals as well. With -j3
it'll only launch 3 the terminals at once and wait for one to exit before starting another one.
COMMANDS=(
'ls; sleep 5'
'ls; sleep 5'
'ls; sleep 5'
'ls; sleep 5'
'echo "hello world"; sleep 5'
'ls; sleep 5'
'echo "hello world"; sleep 5'
)
parallel -j3 gnome-terminal -q --geometry="105x8" --wait -- sh -c ::: "${COMMANDS[@]}"
# -j3 = launch 3 jobs at once
Adding --wait
causes the terminal process to NOT fork to the background. If it forked, it would just launch everything at the same time, negating our attempt at job control.
Upvotes: 0