140

I have three scripts I need to run when I start up my Ubuntu machine, they start services I use in my development environment.

To do that, I manually open three terminals and type in the commands.

Is there any way to create a script that will open three terminals and execute one command in each of these? (Each command should be in a separate terminal window so I can see their output).

138
gnome-terminal -e command

or

xterm -e command

or

konsole -e command

Pretty much

terminal -e command

To make the terminal stay when the command exits:

In konsole there is a --noclose flag.

In xterm, there is a -hold flag.

In gnome-terminal, go to Edit -> Profile Preferences -> Title. Click the Command tab. Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with

gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
  • 1
    If I try to hold the terminal, I get "child process exited normally with status code 127" – Darshan Chaudhary Aug 12 '16 at 7:49
  • 2
    gnome-terminal does not have the title option any more :( – törzsmókus Feb 22 '17 at 13:04
  • 1
    @törzsmókus it is 2017 indeed! LTS releases have a 5 year support life. 14.04 does not end until April 2019. wiki.ubuntu.com/Releases – bhass1 Mar 5 '17 at 22:33
  • 1
    gnome-terminal -e command only works if command is quoted. So this does not work: gnome-terminal -e "echo hello world; sleep 3" but this does: gnome-terminal -e "bash -c 'echo hello world; sleep 3'". Sigh. – bgoodr Apr 16 '17 at 19:47
  • 3
    You can also hold the gnome-terminal open by adding ; $SHELL to the end of the command. Ex: gnome-terminal --tab --title="test" --command="bash -c 'cd /etc; ls; $SHELL'" – Gabriel Staples Apr 20 '18 at 0:46
58

Instead of hard-coding gnome-terminal, konsole, et cetera, use the Alternatives system. The program that executes the default terminal emulator is:

x-terminal-emulator

On my system, it opens a new instance of Konsole every time I execute this command.

Luckily, the terminals seems to support the -e option for executing a command (I verified it for konsole and gnome-terminal). Arguments after the command are passed to the invoked command. Bash refuses to stay open in my terminal, an additional script is needed to get a terminal:

#!/bin/sh
"$@"
exec "$SHELL"

If you've saved the previous script as /home/user/hacky and made it executable, you would run your scripts with:

x-terminal-emulator -e /home/user/hacky your-script optional arguments here

The full path is required and /home/user/hacky has to be executable.

My previous attempt to run a script in a new terminal window can be found in revision #2, it was before I realised arguments can be passed to x-terminal-emulator.

  • In this case, it won't help much as the asker wants to do something that isn't the same for all terminals. – nickguletskii Jun 2 '11 at 20:36
  • Attempt #3: this one should keep the terminal open and run the program with optional arguments. – Lekensteyn Jun 2 '11 at 21:06
  • 1
    I used gnome option, however, once I run my script, the main terminal closes !! .. any idea why ? – McLan May 20 '15 at 14:33
  • 3
    @Suda.nese That is by design, when the "terminal" is done executing the script it will quit because there is nothing more to do. You could "fix" this by invoking a shell where you can execute commands (bash) or have a line such as read -p "Press Return to continue". – Lekensteyn May 20 '15 at 15:44
  • 1
    How can you run more than one command in the terminal? For example cd xxx && start.sh. The interpretor sees the && as the second part of the command (which is logical), but if I quote it, then it tries to exec the whole thing as one big argument – Richard May 25 '17 at 7:02
9

Quite simply-

#!/bin/bash

/etc/init.d/ccpd status

This is enough for other commands that do not need to display anything in terminal. But here one has to see the status displayed.
So, it needs to run in a terminal window

#!/bin/bash

gnome-terminal -e "/etc/init.d/ccpd status"  --window-with-profile=NAMEOFTHEPROFILE

The other post intended [] to be a placeholder

Here "NAMEOFTHEPROFILE" is to be replaced with the name of the profile that "Holds the terminal when the command exits".

enter image description here

enter image description here

  • 1
    @cipricus i believe [] was just placeholder – Karthik T Jan 23 '13 at 9:37
  • got it. but i have to make the terminal not to close so fast. i guess that is also in the linked question – user47206 Jan 23 '13 at 9:43
  • @cipricus have you tried the profile one? Just need to add --window-with-profile=NAMEOFTHEPROFILE to what I have given – Karthik T Jan 23 '13 at 9:44
  • 1
    @cipricus I would have to get back home to give better instructions, but the idea is to create a special profile with that option set, and use the name of the special profile in the place above. – Karthik T Jan 23 '13 at 9:51
  • 2
    @cipricus if that is enough for you, then that is ok. Profile is nothing more than a group of settings. You can setup the settings ONLY for use in your script, and not have to use it in all terminals. You can see Edit -> Profiles to see all the profiles you have, and you would add one there which was setup as explained in the post you linked – Karthik T Jan 23 '13 at 15:39
5

Aided by @nickguletskii's answer, and my own comment under his answer, and inspired by @grabantot's upvote of my comment, here's my preferred way to do it, especially when I want the terminal to stay open so I can then manually use it.

Ex. usage: this is really useful to add to your startup programs so this script will run, open a terminal, create and name a tab in the terminal, and run a command for you. Or, you can just add a symlink to this script to your desktop. I use this type of approach so I can double-click a single icon on my desktop and have it open up a bunch of terminals (with various tabs named according to what work I'm going to do in them) and programs to set up my programming environment, for instance, for daily work.

Here's a contrived example, which opens up a single tab, titles it "test", then runs the simple command cd /etc; ls inside it. The $SHELL part at the end forces the shell to stay open so you can then see its output and continue using it (I learned this somewhere else on Stack Overflow):

gnome-terminal --tab --title="test" --command="bash -c 'cd /etc; ls; $SHELL'"

Here's a more complicated example which opens up 3 separate tabs in the same gnome-terminal. This is exactly the type of thing my desktop shortcut does so I can open up a bunch of programming windows at once:

gnome-terminal --tab --title="tab 1" --command="bash -c 'cd /etc; ls; $SHELL'" --tab --title="tab 2" --command="bash -c 'cd ~; ls; $SHELL'" --tab --title="tab 3" --command="bash -c 'cd ~/temp3; ls; $SHELL'"

Here's a breakdown of that command above:

  • gnome-terminal = open up a gnome-terminal
  • --tab = open up a unique tab for what comes next
  • --title="tab 1" = title this tab "tab 1"
  • --command="bash -c 'cd /etc; ls; $SHELL'" = run the bash -c 'cd /etc; ls; $SHELL' command, which is a command I just made up as an example; here's what it does:
    • bash -c says it is a bash 'c'ommand
    • cd /etc = 'c'hange 'd'irectory into the "/etc" path
    • ls = 'l'i's't contents of this directory
    • $SHELL = this cryptic tidbit is required to keep the shell open so you can work with it. If you want the shell to open, run your command, then close, simply remove this part. I, however, want the tab to stay open so I can go make programming magic. :)
  • we then start back over at the --tab part to produce tab 2, then again for tab 3. Customize to your heart's content.

Screenshot:

enter image description here

  • 1
    glad I was helpful) I also have scripts that I can just click on and start working on the project. There were two problems with them: lots of terminal windows (had a whole separate screen for them) and windows closing after server crashes for example. That answer solves both of my problems with --tab + $SHELL. Nice – grabantot Dec 31 '18 at 9:33
1

commenting for the answer by Lekensteyn. I know this is a old post, but for anyone who finds this useful (as I just did) Instead of making another "hacky script" with just put a function inside the script you are calling

hacky_function()
{
"$@"
exec "$SHELL"
}

Call your script with "x-terminal-emulator -e /path/to/script hacky_function optional arguments here"

Don't forget to put "$@" at the end of the script

-2

Use the screen command and -d detach from an existing screen session, and reattach here -m force a new screen session -S create a named session instead of using the default name

  • this answer is not clear at all, please care to make more understandable – azerafati Apr 20 '17 at 12:18
  • @azerafati indeed, screen doesn't open up any terminal window... is not even intended by that awesome package... – erm3nda Jul 12 '18 at 19:55

protected by Community Nov 25 '14 at 15:04

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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