13

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.

0

2 Answers 2

11

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.

6
  • IIRC terminator has a habit of writing current config to disk when quitting, so if I make changes to the config using an editor while terminator is still running, and then restart terminator, I get the pleasant surprise of seeing none of my modifications take effect.
    – muru
    Jan 9, 2017 at 14:02
  • @muru there are some such weird things yes. I think I had to close all open terminal windows when first creating the new layout. Hang on, let me add a note about that.
    – terdon
    Jan 9, 2017 at 14:03
  • Although you are missing the command to create the config.bak.
    – Ben Watson
    Jan 9, 2017 at 14:39
  • @BenWatson no, the sed command creates the config.bak. That's what the -i.bak does.
    – terdon
    Jan 9, 2017 at 14:59
  • That's interesting, I had to add a line to make the bak else it wasn't resetting properly. I'll investigate sometime.
    – Ben Watson
    Jan 9, 2017 at 15:04
5

Alternative:

Since you've mentioned that you'd accept alternative solution, consider using gnome-terminal. In the past I've written scripts for that, which allow spawning window with multiple tabs and running their own commands. Please see this for example: https://askubuntu.com/a/863179/295286

As for terminator itself, it seems like it allows spawning new tabs for already existing window via --new-tab option, but I see no option to spawn multi-tab window with individual commands other than via --layout option. You can automate it, of course, since layouts are all go into ~/.config/terminator/config file, so you could write out custom config file. I'll try to write one if I won't forget, but I think gnome-terminal approach is far simpler and is exactly what you want.

Original answer

Terminator has a layout preference, which you can use with specific number of tabs, splits, and assign specific color profiles to them. What you want to do is to open window , add how ever many tabs you want, and then right-click, go to Preferences , layouts tab, and click add button

enter image description here

Once you're done editing layout, save it. Now you can call use terminator -l <Layout Name> to spawn window with that exact set of tabs. For instance, from my screenshot you can see I have custom layout, so I would use terminator -l custom

6
  • That seems like a step forward thanks, however if I add -l custom to my four terminator commands, I just end up with four separate windows (each of which is split into four views), with only the top-left view of each populated. How can I force each command to go into the same window?
    – Ben Watson
    Jan 9, 2017 at 9:21
  • @BenWatson Take a look at the screenshot. You can assign custom command to each terminal tab. I would recommend that you use it like so : bash -c "COMMAND 1 ; bash" and just repeat that for each command. That way we're ensuring the window won't close if the command is short-lived Jan 9, 2017 at 9:30
  • I can't use the Terminator custom commands box because the script does a few other things beforehand to populate the <COMMAND> fields. Of course I could have the custom command: box run scripts themselves... It's not massively elegant as I can't just tell others to check out and run one script, but it could be the best solution. I'll give it a day to see if anyone else has a solution, else I'll select this. Thanks.
    – Ben Watson
    Jan 9, 2017 at 9:35
  • I'll take a look at man page, will let you know if i come up with something. As alternative, gnome-terminal can do what you want, i know that for a fact because I've written scripts like that myself before Jan 9, 2017 at 9:38
  • @BenWatson that isn't exactly clear, I agree with terdon, but I sort of know what you are trying to do. I'd sure recommend you edit the question - other users might find your question unclear and close it Jan 9, 2017 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.