18

I'm trying to create a command that will open 3 services from the Rails stack, rails s, spork, and autotest. I can run these commands individually by opening 3 terminals, going to the app folder and running them. I can even open 3 new tabs and run the commands on each.

The problem is that they fail when I try to open the tabs via gnome-terminal like so:

gnome-terminal --tab --title="rails s" -e "rails s" --tab --title="spork" -e "spork" --tab --title="autotest" -e "autotest"

Error (for spork and autotest):

There was an error creating the child process for this terminal
Failed to execute child process "spork" (No such file or directory)

Surprisingly it does work for 'rails s' (the tab shows the server running).

Any ideas?

1
  • Note that -e has been deprecated in newer versions of gnome-terminal, which makes it more tricky to answer the question. To prepare for the deprecation, the best I could come up with is this solution.
    – bluenote10
    Mar 4, 2021 at 10:48

4 Answers 4

17

Invoking gnome-terminal three times and stringing the commands together with && won't work, and the tabs won't appear in the same terminal, and indeed you will have to quit the first terminal for the next to appear.

The way to make sure all your tabs open in one instance of gnome-terminal all at the same time is to use the --tab-with-profile=PROFILENAME switch. In the command below you can leave the first command as gnome-terminal --tab as that it is the first window to be generated; now all you need to know is which profile you are using and you can use --tab-with-profile=YourProfile to invoke the other commands and they will all appear at once in the original terminal opened.

Here is your command modified:

gnome-terminal --tab --title="rails s" -e "rails s" --tab-with-profile=Default --title="spork" -e "spork" --tab-with-profile=Default --title="autotest" -e "autotest"

(For future readers: substitute the program names given after -e to test out the command line given here; remember your targets for -e must be installed and in $PATH; for things not in $PATH use an absolute path such as, for example, /opt/mike/program)

Remember to specify the actual profile you are using for the --tab-with-profile switches. The first invocation of gnome-terminal must use --tab and all the rest --tab-with-profile. With this method it should be possible to open a large number of tabs all in the same instance of gnome-terminal.

Find the profile you are currently using by right clicking in gnome-terminal and look at profiles and there will be a marker on the one you are currently using:

enter image description here

Go to Profile preferences for more information and to check the name of the profile.

For more information, see man gnome-terminal and the Ubuntu manpages online.


Note:

If you have trouble launching your programs with gnome-terminal, either add the location to $PATH, or make a symlink and place it in a $PATH location, or (what was very useful here): create a simple bash wrapper script and call that in the gnome-terminal command line above. (You must call it with an absolute path: i.e. /location/of/script and not just the name of it.)

As an example:

#!/bin/bash
cd $HOME/.rvm/gems/ruby-2.0.0-p0/bin 
spork
exit 0

Then name the script, make it executable and call it in the gnome-terminal command line above. Do this for all the non repo programs that are problematic.

10
  • How do I know the profile? (I tried with Default, my username, and ~/.profile)
    – Manuel
    Apr 3, 2013 at 17:01
  • It says Default but I get the same error using --tab-with-profile=Default
    – Manuel
    Apr 3, 2013 at 17:08
  • @Manuel The other possibility is the target commands aren't in your $PATH;specify the location of spork and the others with an absolute path.
    – user76204
    Apr 3, 2013 at 17:15
  • The command I'm using is gnome-terminal --tab --title="rails s" -e "rails s" --tab-with-profile=Default --title="spork" -e "spork" --tab-with-profile=Default --title="autotest" -e "autotest" (copy/paste from your answer). About $PATH, if this is the problem then why does it work on a tab opened via the menu?
    – Manuel
    Apr 3, 2013 at 17:29
  • @Manuel As you say in your question, you have to go to "the app folder" and run them from there, which means they are not in $PATH. The method in the answer is the one I use to launch all repository installed command-line programs in different tabs in the same gnome-terminal window, and it always works, as those other programs are in $PATH.
    – user76204
    Apr 3, 2013 at 17:44
10

Below is an answer from stackoverflow.com

  1. Add a eval "$BASH_POST_RC" to the end of your .bashrc

  2. Set the BASH_POST_RC environment variable for each tab to that command you like to execute, e.g.: gnome-terminal --working-directory="/home/zardoz/projects/my_rails_app" --tab -e 'bash -c "export BASH_POST_RC=\"rails server\"; exec bash"' --tab -e 'bash -c "export BASH_POST_RC=\"autotest\"; exec bash"'

0
1

gnome-terminal --tab -- sh -c "python3 'test.py' ; bash"

this code open new tab in terminal running a python script and when its over, this new tab will not close, because i added 'bash' in the end of this code...

if you want it multiple times, put it into a while loop in your bash script

0

As discussed in other Q&A, the only straightforward, non-deprecated way to open multiple tabs is to issue the command multiple times, such as the answer at Unix & LInux to the question, Opening new gnome-terminal (v3.28+) with multiple tabs and different commands, by Владимир Савостин.

gnome-terminal -- bash -c "myCommand -some-args; bash"
gnome-terminal -- bash -c "myOtherCommand -some-args; bash"

If I want to extract a longer expression from each command, it seems I had to create a script and call that, which I did in place of using ; bash to keep the window open. I used a simple run_cmd.sh:

#! /bin/bash

set -euo pipefail

display_success() {
  read -p "Exited with success; press enter to exit..."
}

display_error() {
  read -p "Exited with error value $1; press enter to exit..."
}

run_cmd() {
  echo "Running: $@"
  "$@" && display_success || display_error $?
}

run_cmd "$@"

I then replaced bash -c with run_cmd.sh. Since that script is trivial, please consider it as in the public domain.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .