29

I have a script which runs in the background (without terminal windows or TTY) and occasionally does something.

I now want it to do another thing when it does something and it is to open a Gnome Terminal window and execute 2 commands. Actually, I only need to execute 1 command but I want to make the terminal window stay open so I can see the output of the command. The command prints both on stdout and stderr and its output changes over time, so just writing it to a file and sending some kind of notification wouldn't do the job very well.

I can get Gnome Terminal to open a window and execute 1 command:

gnome-terminal -e "sleep 10"

I chose sleep as the long-running command for simplicity. However, when adding another command, no terminal window opens:

gnome-terminal -e "echo test; sleep 10"

What's the solution to this?

1

3 Answers 3

33

gnome-terminal treats everything in quotes as one command, so in order to run many of them consecutively you need to start interpreter (usually a shell), and do stuff inside it, for instance:

gnome-terminal -e 'sh -c "echo test; sleep 10"'

BTW, you may want the window to stay open even after commands finish their job, in such case just start new shell, or replace a current with the new one:

gnome-terminal -e 'sh -c "echo test; sleep 10; exec bash"'
2
  • 4
    # Option “-e” is deprecated and might be removed in a later version of gnome-terminal.# # Use “-- ” to terminate the options and put the command line to execute after it.# That error happens now. But if you replace -e with --, the new terminal that comes up gives an error. How to correctly use --? Commented Mar 12, 2019 at 13:09
  • 15
    @eric.frederich based on this answer to a question at stackoverflow, I believe the syntax should be gnome-terminal -- /bin/sh -c 'echo test; sleep 10'
    – Trevor
    Commented Mar 26, 2019 at 6:19
21

As of January 2020, the -e option in gnome-terminal still works, but throws the warning

# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.

# Use “-- ” to terminate the options and put the command line to execute after it.

You can run the two commands without warning with

gnome-terminal -- /bin/sh -c 'echo test; sleep 10'

And, as mentioned in this answer, if you want the window to stay open afterwards, you can do

gnome-terminal -- /bin/sh -c 'echo test; sleep 10; exec bash'

*answer to a similar question

0

I had better luck placing my commands into an external script, and then running them from the gnome-terminal based 2nd script

script 1: commands.sh

#bin/bash
precommand
app
postcommand

then a script to execute those within gnome-terminal runscript.sh:

gnome-terminal --full-screen --hide-menubar -- /bin/sh commands.sh

You must log in to answer this question.

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