Ubuntu – Spawn new Terminator tabs from Bash script

bashscriptsterminator

I've written a Bash script that runs four long-running (20-30 minutes each) processes:

terminator -T "<NAME-1>" -e "<COMMAND-1>" &
terminator -T "<NAME-2>" -e "<COMMAND-2>" &
terminator -T "<NAME-3>" -e "<COMMAND-3>" &
terminator -T "<NAME-4>" -e "<COMMAND-4>" &

(where <COMMAND-X> is a value created by several lines of Bash beforehand, including calls to Kubernetes).

When I run my script, each of these processes opens in a new Terminator window. To avoid having four new windows spawn each time I run the script, I'd actually like them to all start in different views of a single new window. Is there any way to do this? There's nothing in the manual.

I'd accept a solution that uses a different terminal.

Best Answer

First, open a terminator instance, split it into 4 panes as desired and save as a new layout (make sure to close all open terminal windows except the one you've set up with your new profile). For each of the 4 panes, add a custom command but just add the text COMMAND1, COMMAND2 etc. Save the new layout as foo (or whatever). At this point, your ~/.config/terminator/config should look something like this:

[global_config]
  enabled_plugins = LaunchpadCodeURLHandler, APTURLHandler, LaunchpadBugURLHandler
  focus = mouse
  suppress_multiple_term_dialog = True
  title_use_system_font = False
[keybindings]
[layouts]
  [[default]]
    [[[child0]]]
      fullscreen = False
      last_active_term = ad14f6cd-bcc3-4a80-9852-3fe64f7fc2eb
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      size = 735, 474
      title = sometitle
      type = Window
  [[foo]]
    [[[child0]]]
      fullscreen = False
      last_active_term = 18ebf009-c427-4c2f-8017-78c379a72d31
      last_active_window = True
      maximised = False
      order = 0
      parent = ""
      position = 2730:254
      size = 735, 474
      title = NAME-1
      type = Window
    [[[child1]]]
      order = 0
      parent = child0
      position = 237
      ratio = 0.5
      type = VPaned
    [[[child2]]]
      order = 0
      parent = child1
      position = 368
      ratio = 0.500680272109
      type = HPaned
    [[[child5]]]
      order = 1
      parent = child1
      position = 368
      ratio = 0.500680272109
      type = HPaned
    [[[terminal3]]]
      command = COMMAND1
      order = 0
      parent = child2
      profile = default
      title = NAME-1
      type = Terminal
      uuid = 01e46a5a-0a12-4861-a81e-e376af6041a7
    [[[terminal4]]]
      command = COMMAND2
      order = 1
      parent = child2
      profile = default
      type = Terminal
      uuid = 094d544d-944c-48a7-9960-00370253e799
    [[[terminal6]]]
      command = COMMAND3
      order = 0
      parent = child5
      profile = default
      type = Terminal
      uuid = fe19f601-1963-4af7-8870-f90e289d3d27
    [[[terminal7]]]
      command = COMMAND4
      order = 1
      parent = child5
      profile = default
      type = Terminal
      uuid = 18ebf009-c427-4c2f-8017-78c379a72d31
[plugins]
[profiles]
  [[default]]
    cursor_color = "#aaaaaa"
    font = Monaco 11
    scrollback_infinite = True
    scrollback_lines = 5000
    use_system_font = False

Now, modify your bash script to replace COMMANDN with the actual command you need to run:

#!/bin/bash

## change these to whatever you actually need
command1="echo one; bash"
command2="echo two; bash"
command3="tail -f /var/log/messages; bash"
command4="top"

## Modify terminator's config
sed -i.bak "s#COMMAND1#$command1#; s#COMMAND2#$command2#; s#COMMAND3#$command3#; s#COMMAND4#$command4#;" ~/.config/terminator/config

## Launch a terminator instance using the new layout
terminator -l foo

## Return the original config file
mv ~/.config/terminator/config.bak ~/.config/terminator/config

Running that script will now open a new terminal window with 4 panes, each running one of your commands.

Important: if the command is something that runs and then exits (as opposed to something like top which runs until you tell it to stop), you need COMMAN; bash as the command so that a new shell will be started. Otherwise, the command will run and the shell will exit immediately. That's why I have ; bash after each command except top in y script.